【iOS】UIDynamicAnimator动画

创建动画

1 UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

协议代理

1 @protocol UIDynamicAnimatorDelegate <NSObject>
2
3 @optional
4 - (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;
5 - (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;
6
7 @end

属性

1 // 行为执行时间
2 @property (nonatomic, readonly) NSTimeInterval elapsedTime;
3 // 判断是否正在执行
4 @property (nonatomic, readonly, getter = isRunning) BOOL running;

设置动画组件Item的动力属性

UIDynamicItemBehavior

 1 UIDynamicItemBehavior *dynamic = [[UIDynamicItemBehavior alloc] init];
 2 [animator addBehavior:dynamic];
 3 [dynamic addItem:view];
 4
 5 // 相关属性
 6 @property (readwrite, nonatomic) CGFloat elasticity; // Usually between 0 (inelastic) and 1 (collide elastically)
 7 @property (readwrite, nonatomic) CGFloat friction; // 0 being no friction between objects slide along each other
 8 @property (readwrite, nonatomic) CGFloat density; // 1 by default
 9 @property (readwrite, nonatomic) CGFloat resistance; // 0: no velocity damping
10 @property (readwrite, nonatomic) CGFloat angularResistance; // 0: no angular velocity damping

为动画组件添加具体行为

吸引行为 UISnapBehavior

1 UISnapBehavior *snap = [[UISnapBehavior alloc]
2                         initWithItem:view
3                         snapToPoint:CGPointMake(200, 300)];
4 snap.damping = 0.9;//阻尼系数
5 [animator addBehavior:snap];

重力行为 UIGravityBehavior

1 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
2 // 重力矢量方向 默认为 (0,1)
3 gravity.gravityDirection = CGVectorMake(0, 1);
4 // 重力大小
5 gravity.magnitude = 5;
6 [animator addBehavior:gravity];
7 [gravity addItem:view];

碰撞行为 UICollisionBehavior

1 UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
2 // 边界刚体碰撞
3 collision.translatesReferenceBoundsIntoBoundary = YES;
4 [animator addBehavior:collision];
5 [collision addItem:view];

作用力行为 UIPushBehavior

1 UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[view]
2                                                         mode:UIPushBehaviorModeInstantaneous];
3 //    UIPushBehaviorModeContinuous 持续作用力
4 //    UIPushBehaviorModeInstantaneous 瞬间作用力
5 push.active = YES;
6 push.pushDirection = CGVectorMake(1, 0);
7 [animator addBehavior:push];

效果演示

UIDynamicItemBehavior+UIGravityBehavior+UICollisionBehavior

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5
 6     animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
 7
 8     dynamic = [[UIDynamicItemBehavior alloc]init];
 9     dynamic.elasticity = 0.7;// 弹力系数
10     [animator addBehavior:dynamic];
11
12     gravity = [[UIGravityBehavior alloc]init];
13     gravity.gravityDirection = CGVectorMake(0, 1);// 重力矢量方向
14     [animator addBehavior:gravity];
15
16     collision = [[UICollisionBehavior alloc]init];
17     collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞
18     [animator addBehavior:collision];
19 }
20
21 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
22 {
23     CGFloat width = self.view.frame.size.width;
24
25     int x = arc4random()%(int)width;
26     int z = arc4random()%20;
27
28     UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, 10, 20+z, 20+z)];
29     view.backgroundColor = [UIColor greenColor];
30     view.layer.borderColor = [UIColor blueColor].CGColor;
31     view.layer.borderWidth = 1.0;
32     view.layer.masksToBounds = YES;
33     [self.view addSubview:view];
34
35
36     [dynamic addItem:view];
37     [gravity addItem:view];
38     [collision addItem:view];
39 }

UIPushBehavior+UIGravityBehavior

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5
 6     animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
 7
 8     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
 9     [self.view addGestureRecognizer:tap];
10
11     gravity = [[UIGravityBehavior alloc] init];
12     gravity.gravityDirection = CGVectorMake(0, 1);
13     gravity.magnitude = 10.0;
14     [animator addBehavior:gravity];
15 }
16
17 - (void)tap:(UITapGestureRecognizer *)tap
18 {
19     CGPoint point = [tap locationInView:self.view];
20
21     for (CGFloat i = 0 ; i < M_PI*2; i = i + 0.2)
22     {
23         UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
24         view.center = point;
25         view.backgroundColor = [UIColor blueColor];
26         view.layer.cornerRadius = 5;
27         view.layer.masksToBounds = YES;
28         [self.view addSubview:view];
29
30         UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[view]
31                                                                 mode:UIPushBehaviorModeInstantaneous];
32         push.active = YES;
33         push.angle = i;
34         push.magnitude = 0.05;
35         [animator addBehavior:push];
36
37         [gravity addItem:view];
38     }
39 }

UIDynamicItemBehavior+UICollisionBehavior+UISnapBehavior

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5
 6     animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
 7
 8     dynamic = [[UIDynamicItemBehavior alloc]init];
 9     dynamic.elasticity = 0.7;// 弹力系数
10     [animator addBehavior:dynamic];
11
12     collision = [[UICollisionBehavior alloc]init];
13     collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞
14     [animator addBehavior:collision];
15
16     CGFloat width = self.view.frame.size.width;
17     CGFloat height = self.view.frame.size.height;
18
19     red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
20     red.center = CGPointMake(width/2, height/2+100);
21     red.backgroundColor = [UIColor redColor];
22     red.layer.cornerRadius = 15.0;
23     red.layer.masksToBounds = YES;
24     [self.view addSubview:red];
25
26     [collision addItem:red];
27
28     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
29     [self.view addGestureRecognizer:tap];
30 }
31
32 - (void)tap:(UITapGestureRecognizer *)tap
33 {
34     CGPoint point = [tap locationInView:self.view];
35
36     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
37     view.center = point;
38     view.backgroundColor = [UIColor blueColor];
39     view.layer.cornerRadius = 10.0;
40     view.layer.masksToBounds = YES;
41     [self.view addSubview:view];
42
43     [collision addItem:view];
44
45     UISnapBehavior *snap = [[UISnapBehavior alloc]
46                             initWithItem:view
47                             snapToPoint:red.center];
48     snap.damping = 0.1;
49     [animator addBehavior:snap];
50
51     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
52         [animator removeBehavior:snap];
53     });
54 }

时间: 2024-07-31 14:31:25

【iOS】UIDynamicAnimator动画的相关文章

[iOS]过渡动画之高级模仿 airbnb

注意:我为过渡动画写了两篇文章:第一篇:[iOS]过渡动画之简单模仿系统,主要分析系统简单的动画实现原理,以及讲解坐标系.绝对坐标系.相对坐标系,坐标系转换等知识,为第二篇储备理论基础.最后实现 Mac 上的文件预览动画.第二篇:[iOS]过渡动画之高级模仿 airbnb,主要基于第一篇的理论来实现复杂的界面过渡,包括进入和退出动画的串联.最后将这个动画的实现部分与当前界面解耦,并封装为一个普适(其他类似界面也适用)的工具类. 这两篇文章将会带你学到如何实现下图 airbnb 首页类似的过渡动画

iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题

本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37  segmentfault-博客原文  http://segmentfault.com/blog/alan/1190000002411296 iOS的各种动画相漂亮,相信这是吸引很多人买iPhone的原因之一.不仅如此,这还是吸引我做iOS开发的一大原因,因为在iOS上给界面实现一些像样的动画实在是太轻松了! 这里就介绍一下iOS的block-based an

iOS核心动画

iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看到动画操作在iOS中是如何简单和高效,很多原来想做但是苦于没有思路的动画在iOS中将变得越发简单: CALayer CALayer简介 CAL

iOS核心动画中的常用类型

CATransaction 当我们在自定义的图层上修改某些支持动画的属性时,系统会为该属性的修改自动产生动画.这种其实属于隐式动画.隐式动画要得益于CATransaction. 一个CATransaction从调用CATransaction.begin()开始,以CATransaction.commit()结束.在这其间对图层属性的修改,会受该Transaction的控制,可以通过setAnimationDuration修改Transaction的duration. 系统的隐式动画是因为在Run

iOS核心动画学习整理

最近利用业余时间终于把iOS核心动画高级技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,对应其中一些知识做了相应的整理,整理为demo(https://github.com/PurpleSweetPotatoes/Layer_learn).此demo中都是基于教程书籍中的编程示例,并加上了注解以方便各位iOS爱好者学习使用. 在这里利用此教程中的基础知识做了2个小demo,活动指示器效果和火焰效

iOS开发——动画OC篇&amp;所有常用动画总结

所有常用动画总结 先来装下B,看不懂没关系,其实我也看不懂-?? iOS provides several different frameworks for adding graphics and animations to your apps. UIKit is an Objective-C API that provides basic 2D drawing, image handling, and ways to animate user interface objects. Core G

iOS核心动画高级技巧之核心动画(三)

iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结(五) 隐式动画 隐式动画主要作用于CALayer的可动画属性上面,UIView对应的layer是不可以的,只要你改变属性的值,它不是突兀的直接改变过去,而是一个有一个动画的过程,这个时间等属性你可以通过事务(CATransaction)来控制,如果你不自己提供一个事务,它的默认时间是0.25秒,当然

IOS 动画专题 --iOS核心动画

iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看到动画操作在iOS中是如何简单和高效,很多原来想做但是苦于没有思路的动画在iOS中将变得越发简单: CALayer CALayer简介 CALayer常用属性 CALayer绘图 Core Animation 基础动画 关键帧动画 动画组 转场动画 逐帧动画 UIView动画封装目 录 基础动画 关

iOS中动画的简单使用

//***简单动画 [UIView animateWithDuration:3 animations:^{ //      _animationView.center = CGPointMake(arc4random()%320, arc4random()%480);//改变中心点 //      _animationView.frame = CGRectMake(arc4random()%320, arc4random()%480,arc4random()%320, arc4random()%

iOS核心动画工作笔记

1.图层和UIVIew的区别:图层不能和用户进行交互,图层性能高 2.imageVIew的图片显示是在图层上面的子层.用maskBounds剪切时剪的是图层,用户看不到是因为子层挡住了 3.CAlayer的代理方法没有协议,任何对象都能成为他的代理,即NSObject的方法 4.UIVIew内部的根图层的代理就是View本身,所以在UIVIew中的drawRect方法绘图.一个view不能设置代理.因为已经是它图层的代理 5.Core Animation直接作用于CALayer,缺点是动画后图片