UIDynamic 物理动效

一 什么是UIDynamic Animation
UIDynamic Animation是IOS 7引入的一个动态库,用来模拟现实世界的物理模型,隶属于UIKit。
主要模拟以下几种物理行为:

  • UIGravityBehavior
  • UIAttachmentBehavior
  • UISnapBehavior
  • UIPushBehavior
  • UICollisionBehavior
  • UIDynamicItemBehavior

二 如何创建一个UIDynamic Animation
(1)创建一个UIDynamicAnimator,大部分情况下只需要一个Animator
(2)创建一个或者多个UIDynamic Behavior,并添加到Animator
(3)为Dynamic Behavior添加Dynamic Item。

三 UIGravityBehavior -重力
几个要配置的属性
magnitude - 重力加速度值 默认情况下1000points/second^2
angle - 重力的方向(由弧度指定)
gravityDirection - 重力的方向(由vector指定,默认(0.0,1.0))
模拟重力:

UIAttachmentBehavior * attachBeahavior =  [[UIAttachmentBehavior alloc] initWithItem:self.imageview attachedToAnchor:CGPointMake(CGRectGetMidX(self.view.frame), 114)];   
UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.imageview]];    
[self.animator addBehavior:attachBeahavior];    

四 UIDynamicItemBehavior
配置一些公用的属性,与其他的Dynamic Behavior共同配合
addAngularVelocity:forItem: - 添加角速度
addLinearVelocity:forItem: - 添加线速度
allowsRotation - 是否允许旋转
angularResistance - 角速度方向的阻力
density- 相对密度
elasticity - 碰撞的弹性
friction - 摩擦
resistance-线性阻力
五 UICollisionBehavior - 碰撞
为Item与边界以及Item之间添加碰撞效果。

addBoundaryWithIdentifier:forPath: - 添加Path边界 addBoundaryWithIdentifier:fromPoint:toPoint: - 添加线边界
collisionMode - 碰撞模式(三种:只有Item之间;只有边界;边界以及Item之间都存在碰撞效果)
translatesReferenceBoundsIntoBoundary - 把参考View的bounds转换为边界

 UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] init]; //    gravityBehavior.angle = M_PI/2;   
 gravityBehavior.gravityDirection = CGVectorMake(0,1);   
 gravityBehavior.magnitude = 0.5;
 [gravityBehavior addItem:self.imageview]; 
 [self.animator addBehavior:gravityBehavior];

六 UISnapBehavior 震荡行为
震荡行为定义了一个动态运动,达到指定点后实现震荡效果,震荡的数量是可以设置的。
配置属性:
damping - 动画结束的时候的震荡值 0.0-1.0;其中0.0最小,1.0最大。默认0.5

UISnapBehavior * snapbehavior = [[UISnapBehavior alloc] initWithItem:self.imageview snapToPoint:self.view.center]; 
snapbehavior.damping = 0.65;
[self.animator addBehavior:snapbehavior];

七 UIAttachmentBehavior - 吸附
为Items之间或者Item和锚点来创建吸附行为,如果是Item,默认吸附Item的中心,当然可以配置。
initWithItem:attachedToAnchor: - 初始化吸附到锚点
initWithItem:attachedToItem: - 初始化吸附到item
anchorPoint - 锚点
damping - 阻尼(阻力大小)
frequency - 震荡频率
length - 吸附的两个点之间的距离(锚点和Item或者Item之间)

UIAttachmentBehavior * attachBeahavior =  [[UIAttachmentBehavior alloc] initWithItem:self.imageview attachedToAnchor:CGPointMake(CGRectGetMidX(self.view.frame), 114)];
UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.imageview]];
 [self.animator addBehavior:attachBeahavior];
 [self.animator addBehavior:gravityBehavior];

八 UIPushBehavior - 推动
给一个Item持续或者瞬间的推力
initWithItems:mode: - 初始化并且指明是瞬间还是持续的推力
angle - 力的角度
magnitude - 推力大小
pushDirection - 推力方向

UIPushBehavior * push = [[UIPushBehavior alloc] initWithItems:@[self.imageview] mode:UIPushBehaviorModeInstantaneous];
 push.pushDirection = CGVectorMake(45, 0);
 push.magnitude = 1.0;
 UIDynamicItemBehavior * itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.imageview]];
 itemBehavior.resistance = 0.8;
 [self.animator addBehavior:itemBehavior];
 [self.animator addBehavior:push];

文/吴白(简书作者)
原文链接:http://www.jianshu.com/p/9272b8e023cb
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值