创建动画

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核心动画学习整理

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

  2. IOS 核心动画之CAKeyframeAnimation - iBaby

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

  3. iOS各种动画效果

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

  4. IOS之动画

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. <!DOCTYPE> 标签是什么

    DOCTYPE 标签,是html文档的类型声明(document type declaration,所谓声明,也就是宣称我他妈是谁),用来告诉浏览器,使用什么样的文档类型定义(Document Typ ...

  2. Node.js 的安装

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 的运行环境,简单的说就是运行在服务端的 JavaScript.所以学起来还是比较容易接受的. Node.js 使用事件驱动 ...

  3. bootstrap 的模态框的宽与高设置

    1,改变bootstrap 的宽与高, 将style=“height:900px”放在<div class = "modal-dialog">或者更外层上,整个模态框的 ...

  4. How to fix Mysql table crashes

    Whenever you enconter this: Please use mysql_upgrade to fix this error. or using  mysql_upgrade -u r ...

  5. javascript之 原生document.querySelector和querySelectorAll方法

    querySelector和querySelectorAll是W3C提供的新的查询接口,其主要特点如下: 1.querySelector只返回匹配的第一个元素,如果没有匹配项,返回null.  2.q ...

  6. 软工读书笔记 week 5 ——《构建之法》

    本周主要对<构建之法>中的一部分进行阅读. 一.软件与软件工程究竟是什么? 本书的概论部分就指出“软件 = 程序 + 软件工程”.而我们这门课的名字就叫“现代软件工程”.其实在上课之前,我 ...

  7. linux安装redis+RedisDesktopManager

    一:redis简介 (REmote DIctionary Server Redis远程字典服务器)       1:Redis是一个开源的使用ANSI C语言编写.完全免费开源的,遵守BSD协议.支持 ...

  8. pmp心得

    我报名比较晚,等缴费最后期限,才缴费,下定决心,开始正式的备考. 我的工作比较忙,备考时间特比较短,从拿到书到考试只有二个月了,心理慌慌的,期间还有一门其他的考试,在5月底,实际时间只能有20来天. ...

  9. Perl常用语法

    本文主要为了方便阅读.编写perl脚本而记录的常用的命令.语法. 一.条件判断语法 注意,数字 0, 字符串 '0' . "" , 空 list () , 和 undef 为 fa ...

  10. C# 五大修饰符

    修饰符 访问权限 public 关键字是类型和类型成员的访问修饰符. 公共访问是允许的最高访问级别. 对访问公共成员没有限制 private 私有访问是允许的最低访问级别. 私有成员只有在声明它们的类 ...