连带动画UISanpBehavior


简单的效果就是绿色的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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,526评论 25 709
  • 某日凌晨三点,小李子在微信上面问我在不在,我本想无视,连续一周熬夜加班我实在没有精力去搭理他。他直接揭穿,我刚刚看...
    壹分君xi阅读 9,289评论 32 69
  • 最近,我在好姐妹筱的状态中看到有个人说对筱的印象——爱你时你是我的一切,不爱你时你什么都不是!就问筱,这么煽情,谁...
    溪绕麦田阅读 12,547评论 18 24
  • 舍友讨论,你允许另一半精神出轨还是身体出轨,大家都在踊跃发言,有说不同意身体出轨啊,有说不同意精神出轨啊… 其实一...
    最天真无邪善良可爱的葡萄阅读 1,079评论 1 0
  • 建构主义的代表维果茨基强调将思维与学习置于社会情境与文化实践的背景之中。他强调,所有的高级心智过程,如推理与...
    闻君儿阅读 1,852评论 0 0