转自: http://www.xuebuyuan.com/2106652.html
1。与之前的UIPushBehavior不同,之前的Push只是中规中矩的移动
而这个SnapBehavior带来的移动视觉效果更强烈
- (instancetype)initWithItem:(id<UIDynamicItem>)item snapToPoint:(CGPoint)point //初始化,每一个snapBehavior只能关联一个对象
damping //CGFloat ,迅猛度,弹性[0,1]默认0.5
e.g.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self createGestureRecognizer];
[self createSmallSquareView];
[self createAnimatorAndBehaviors];
}
- (void) createGestureRecognizer{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tap];
}
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
CGPoint tapPoint = [paramTap locationInView:self.view];
if (self.snapBehavior != nil){
[self.animator removeBehavior:self.snapBehavior];
}
self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.squareView snapToPoint:tapPoint];
self.snapBehavior.damping = 0.5f; //剧列程度
[self.animator addBehavior:self.snapBehavior];
}
- (void) createSmallSquareView{
self.squareView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
self.squareView.backgroundColor = [UIColor greenColor];
self.squareView.center = self.view.center;
[self.view addSubview:self.squareView];
}
- (void) createAnimatorAndBehaviors{
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.squareView]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];
self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.squareView snapToPoint:self.squareView.center];
self.snapBehavior.damping = 0.5f; /* Medium oscillation */
[self.animator addBehavior:self.snapBehavior];
}