创建动画

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

协议代理

@protocol UIDynamicAnimatorDelegate <NSObject>

@optional
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator; @end

属性

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

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

UIDynamicItemBehavior

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

为动画组件添加具体行为

吸引行为 UISnapBehavior

UISnapBehavior *snap = [[UISnapBehavior alloc]
initWithItem:view
snapToPoint:CGPointMake(, )];
snap.damping = 0.9;//阻尼系数
[animator addBehavior:snap];

重力行为 UIGravityBehavior

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

碰撞行为 UICollisionBehavior

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

作用力行为 UIPushBehavior

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

效果演示

UIDynamicItemBehavior+UIGravityBehavior+UICollisionBehavior

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; dynamic = [[UIDynamicItemBehavior alloc]init];
dynamic.elasticity = 0.7;// 弹力系数
[animator addBehavior:dynamic]; gravity = [[UIGravityBehavior alloc]init];
gravity.gravityDirection = CGVectorMake(, );// 重力矢量方向
[animator addBehavior:gravity]; collision = [[UICollisionBehavior alloc]init];
collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞
[animator addBehavior:collision];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGFloat width = self.view.frame.size.width; int x = arc4random()%(int)width;
int z = arc4random()%; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, , +z, +z)];
view.backgroundColor = [UIColor greenColor];
view.layer.borderColor = [UIColor blueColor].CGColor;
view.layer.borderWidth = 1.0;
view.layer.masksToBounds = YES;
[self.view addSubview:view]; [dynamic addItem:view];
[gravity addItem:view];
[collision addItem:view];
}

UIPushBehavior+UIGravityBehavior

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap]; gravity = [[UIGravityBehavior alloc] init];
gravity.gravityDirection = CGVectorMake(, );
gravity.magnitude = 10.0;
[animator addBehavior:gravity];
} - (void)tap:(UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:self.view]; for (CGFloat i = ; i < M_PI*; i = i + 0.2)
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(, , , )];
view.center = point;
view.backgroundColor = [UIColor blueColor];
view.layer.cornerRadius = ;
view.layer.masksToBounds = YES;
[self.view addSubview:view]; UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[view]
mode:UIPushBehaviorModeInstantaneous];
push.active = YES;
push.angle = i;
push.magnitude = 0.05;
[animator addBehavior:push]; [gravity addItem:view];
}
}

UIDynamicItemBehavior+UICollisionBehavior+UISnapBehavior

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; dynamic = [[UIDynamicItemBehavior alloc]init];
dynamic.elasticity = 0.7;// 弹力系数
[animator addBehavior:dynamic]; collision = [[UICollisionBehavior alloc]init];
collision.translatesReferenceBoundsIntoBoundary = YES;// 边界刚体碰撞
[animator addBehavior:collision]; CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height; red = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
red.center = CGPointMake(width/, height/+);
red.backgroundColor = [UIColor redColor];
red.layer.cornerRadius = 15.0;
red.layer.masksToBounds = YES;
[self.view addSubview:red]; [collision addItem:red]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
} - (void)tap:(UITapGestureRecognizer *)tap
{
CGPoint point = [tap locationInView:self.view]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
view.center = point;
view.backgroundColor = [UIColor blueColor];
view.layer.cornerRadius = 10.0;
view.layer.masksToBounds = YES;
[self.view addSubview:view]; [collision addItem:view]; UISnapBehavior *snap = [[UISnapBehavior alloc]
initWithItem:view
snapToPoint:red.center];
snap.damping = 0.1;
[animator addBehavior:snap]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[animator removeBehavior:snap];
});
}

【iOS】UIDynamicAnimator动画的更多相关文章

  1. [iOS]UIDynamicAnimator动画

    创建动画 UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; 协议代理 ...

  2. iOS核心动画学习整理

    最近利用业余时间终于把iOS核心动画高级技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,对应其中一 ...

  3. IOS 核心动画之CAKeyframeAnimation - iBaby

    - IOS 核心动画之CAKeyframeAnimation - 简单介绍 是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation ...

  4. iOS各种动画效果

    ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDu ...

  5. IOS之动画

    IOS之动画   15.1 动画介绍 15.2 Core Animation基础 15.3 隐式动画 15.4 显式动画 15.5 关键帧显式动画 15.6 UIView级别动画 15.1 动画介绍 ...

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

    iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看 ...

  7. ios 学习动画的套路 (一)

    你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟~不知道从哪里下手去写!会连续的 ...

  8. Bodymovin:Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画

    转自:https://www.cnblogs.com/zamhown/p/6688369.html 大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画 ...

  9. 大杀器Bodymovin和Lottie:把AE动画转换成HTML5/Android/iOS原生动画

    前段时间听部门老大说,Airbnb出了个移动端的动画库Lottie,可以和一个名叫Bodymovin的AE插件结合起来,把在AE上做好的动画导出为json文件,然后以Android/iOS原生动画的形 ...

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

    注意:我为过渡动画写了两篇文章:第一篇:[iOS]过渡动画之简单模仿系统,主要分析系统简单的动画实现原理,以及讲解坐标系.绝对坐标系.相对坐标系,坐标系转换等知识,为第二篇储备理论基础.最后实现 Ma ...

随机推荐

  1. Ruby中有意思的块

    块:是在调用方法时,能与参数一起传递的多个处理的集合 简单点说,跟在方法执行后面的do |变量| end就是一个块,这个块会被传入方法中去执行! 这个非常厉害,非常有意思! 在ruby中,如果需要便利 ...

  2. 将 Eclipse 的配色改为黑底白字

    1.先到 eclipsecolorthemes下载一个主题. 2.Eclipse File-->Import 3.Import视窗内选择 General-->Preferences 4.选 ...

  3. centos下搭建redis集群

    必备的工具: redis-3.0.0.tar redis-3.0.0.gem   (ruby和redis接口) 分析:     首先,集群数需要基数,这里搭建一个简单的redis集群(6个redis实 ...

  4. hdu4764 Stone 博弈

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4764 很水的博弈题目 代码: #pragma comment(linker, "/STAC ...

  5. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

  6. 纯真IP数据库格式详解

    纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...

  7. Java学习笔记——设计模式之一.简单工厂

    蜀道之难.难于上青天,侧身西望长咨嗟 --蜀道难 设计模式第一篇,简单工厂. 定义Operation类 package cn.no1.simplefactory; public abstract cl ...

  8. 省市区三级联动(jquery+ajax)(封装和不封装两种方式)-----2017-05-15

    首先,要实现如下图效果, 1.要理清思路: 先做出三个下拉菜单----根据第一个下拉菜单的value值获取第二个下拉列表的内容,第三个同理. 2.用到的数据库表:Chinastates表 规律:根据国 ...

  9. linux系统管理--查看进程

    关于进程的查看,大家都不会陌生 ,主要是ps和pstree命令. ps  aux    查看系统中所有进程,使用BSD操作系统格式.(注意:不是ps -aux) 执行结果 USER :该进程是由哪个用 ...

  10. 基于Android的上课助手的概况及第一周冲刺详情

    基于Android平台的上课助手 一.       功能简介 课表查询 课程提醒 空闲教室的查询 二.       开发环境 Android 三.       开发成员 组长:李志岩 成员:王亚蕊.孙 ...