1.6甩行為UISnapBehavior
(一)碰撞行為UICollisionBehavior作用
作用:可以將view通過動(dòng)畫甩到某一個(gè)點(diǎn)
(二)常用屬性和方法
// 初始化一個(gè)甩行為
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;
// 具體甩到哪一個(gè)點(diǎn)
@property (nonatomic, assign) CGPoint snapPoint;
// 震蕩幅度 值從0.0到0.1 值越大幅度越小,反之越大 默認(rèn)為0.5
@property (nonatomic, assign) CGFloat damping;
示例代碼:
// 當(dāng)點(diǎn)擊屏幕的時(shí)候?qū)⒁粋€(gè)紅色的view甩到手指所在的點(diǎn)上
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 獲取UITouch對(duì)象
UITouch *t = touches.anyObject;
// 獲得當(dāng)前手指所在屏幕的點(diǎn)
CGPoint p = [t locationInView:self.view];
// 1.創(chuàng)建仿真器
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 2.創(chuàng)建仿真行為
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:p];
// 設(shè)置震蕩幅度 從0.0 到1.0 值越大震蕩幅度越小 ,反之越大 默認(rèn)為0.5
snap.damping = 1;
// 3.將仿真行為添加到仿真器
[self.animator addBehavior:snap];
}