您的位置:首页 > 产品设计 > UI/UE

iOS-UIDynamic物理仿真-捕捉-UISnapBehavior

2016-03-15 01:00 561 查看

捕捉-UISnapBehavior

今天我们来讲物理仿真的另一种行为捕捉行为,不多说先上图



套路和之前物理仿真重力/碰撞的实现是一致的

首先在storyboard拖取一个View,设置一下frame属性,当然偏好纯代码的同学也可也在viewDidload方法中手动创建

@property (weak, nonatomic) IBOutlet UIView *testView;


设置一下控件的圆角,其次给控制器的View添加UITapGestureRecognizer手势对象(点击手势)

self.testView.layer.cornerRadius = 25;

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTapGesture:)];

[self.view addGestureRecognizer:tapGesture];


在viewDidTapGesture:方法中实现点击手势之后的具体操作(捕捉仿真动画)

- (void)viewDidTapGesture:(UITapGestureRecognizer *)tapGesture {
//获取手指当前的触摸点
CGPoint currentTapPoint = [tapGesture locationInView:self.view];

//创建物理仿真器
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

//创建物理仿真行为
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.testView snapToPoint:currentTapPoint];

//设置属性
// snapPoint                                                                捕获的点
// damping; // damping value from 0.0 to 1.0. 0.0 is the least oscillation. 阻尼系数
snap.damping = 0.2;

//添加行为
[self.animator addBehavior:snap];

//改变控件的颜色
self.testView.backgroundColor = [self randomColor];

}


//产生随机色的方法
- (UIColor *)randomColor {
CGFloat red = arc4random() % 256;
CGFloat green = arc4random() % 256;
CGFloat blue = arc4random() % 256;

return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1];
}


最后贴一下源码地址物理仿真
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  物理仿真-捕捉