连带动画UISanpBehavior

连带动画UISanpBehavior_第1张图片

简单的效果就是绿色的view跟随手表的点击,而产生一个类似回弹,并附带粘性的动画.

1.初始化基本控件

- (void) createSmallSquareView{
    UIViews *squareView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
self. squareView = squareView
    self.squareView.backgroundColor = [UIColor greenColor];
    self.squareView.center = self.view.center;
    [self.view addSubview:self.squareView];
}

2.添加手势给控制器view

- (void) createGestureRecognizer{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(snapTap:)];
    [self.view addGestureRecognizer:tap];
}

3.执行手势监听方法

- (void)snapTap:(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];
}

4.对于连带动画必须在初始化的时候进行设置,负责动画不会被执行

- (void) createAnimatorAndBehaviors{
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.squareView snapToPoint:self.squareView.center];
    self.snapBehavior.damping = 0.5f; /* Medium oscillation */
    [self.animator addBehavior:self.snapBehavior];
}

你可能感兴趣的:(连带动画UISanpBehavior)