创建动画

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. 使用Java语言开发微信公众平台(六)

    在上一节课程中,我们来学习了微信公众平台最基础的一个接口--access_token,并且能够从微信公众平台中取到access_token. 那么,在本节课程中,我们要以上节课获取到的access_t ...

  2. 蓝桥杯-打印十字图-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  3. base 镜像 - 每天5分钟玩转容器技术(10)

    上一节我们介绍了最小的 Docker 镜像,本节讨论 base 镜像. base 镜像有两层含义: 不依赖其他镜像,从 scratch 构建. 其他镜像可以之为基础进行扩展. 所以,能称作 base ...

  4. Silverlight的DataGrid合并单元格

    现在也不知道还有没有同学做Silverlight开发了,我是一个Silverlight菜鸟,遇到问题也很难百度查到.就简单的记录一下这两天遇到的问题,并做了一个简单的小Demo,希望能够帮助到其他同学 ...

  5. MySQL对innodb某一个表进行移动

    (步骤:建表, 禁用表空间,复制表空间,重用表空间)   mysql> desc test; +-------+-------------+------+-----+---------+---- ...

  6. sublime Text2 快捷键精华版

      Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+V:粘贴 ...

  7. Azure Event Hub 技术研究系列2-发送事件到Event Hub

    上篇博文中,我们介绍了Azure Event Hub的一些基本概念和架构: Azure Event Hub 技术研究系列1-Event Hub入门篇 本篇文章中,我们继续深入研究,了解Azure Ev ...

  8. 如何优雅地运用 Chrome (Google)

    已经在很多工具类文章前言中,提及使用工具的重要性:以至于在写这篇时候,大为窘迫:穷尽了脑海中那些名句箴言,目测都已然在先前文章中被引用.鉴于杳让人心底意识到工具的重要性,并且能践行之,远比介绍如何使用 ...

  9. Jmeter 初学(一)

    Jmeter 目前属于比较流行的测试工具,即可做自动化测试也可以做性能测试,而且比较方便. 环境准备: Jmeter 运行环境需要跑在java环境,首先需要安装一下java的环境,由于我目前使用的Jm ...

  10. Natas Wargame Level20 Writeup(会话状态注入/篡改)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArMAAACmCAYAAADJYwcaAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF