/** UIDynamic是苹果在iOS7新推出的,在UIKit中能够做物理仿真动画的技术! 一. 使用步骤: 1. 实例化仿真者,并制定参照系视图 */ UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self]; /** 2. 指定动画仿真行为,共有 1> 吸附 UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.box snapToPoint:location]; 2> 推 [[UIPushBehavior alloc] initWithItems:@[self.box] mode:UIPushBehaviorModeInstantaneous]; 推行为可以分为单次推,和持续推两种动作,如果是单次推,需要:_push.active = YES; // 推力的角度,方向 _push.angle = angle; // 力量的大小 _push.magnitude = distance / 10.0; // 对于单次推动,一定要设置成YES,才能够发力 _push.active = YES; 3> 附着、附加行为 UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.box offsetFromCenter:offset attachedToAnchor:anchor]; 包括:刚性附加行为和弹性附加行为,指定以下两个属性即可实现弹性附加行为: // 振幅数值越小,震动越大 self.attachment.damping = 0.1; self.attachment.frequency = 1.0f; 4> 重力行为 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[blueView]]; 默认是向下运动,从屏幕顶部运动出屏幕大概需要1s的时间 5> 碰撞行为 UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[blueView]]; // 启用碰撞边界 collision.translatesReferenceBoundsIntoBoundary = YES; 如果存在物体需要碰撞,又不能移动的(墙壁,挡板),可以使用边界来实现: [collision addBoundaryWithIdentifier:@"lalala" fromPoint:redView.frame.origin toPoint:CGPointMake(toX, toY)]; 通过代理方法: - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p 可以做碰撞后的处理,例如小星星! 6> 物体属性行为,可以指定参与仿真物体的物理参数,例如:密度,弹性系数,摩擦系数等,通常不用管 UIDynamicItemBehavior *item = [[UIDynamicItemBehavior alloc] initWithItems:@[blueView]]; ====================================================================== 应用场景: 在应用程序开发中, 吸附行为 => 按钮出现的效果,譬如新浪微博写微博的6个按钮 重力行为 => 在锁屏时,上拉相机,不到位时松手 附加行为 => iPhone的短信清单,在快速滑动松手时,会有一个所有短信挤到一起,然后再逐渐张开的效果 */