UIKit动⼒力学最⼤大的特点是将现实世界动⼒力驱动的动画引⼊入了UIKit, ⽐比如重⼒力,铰链连接,碰撞,悬挂等效果,即将2D物理引擎引⼊入了 UIKit
• 注意:UIKit动⼒力学的引⼊入,并不是为了替代CA或者UIView动画,在 绝⼤大多数情况下CA或者UIView动画仍然是最优⽅方案,只有在需要引 ⼊入逼真的交互设计的时候,才需要使⽤用UIKit动⼒力学它是作为现有交互 设计和实现的⼀一种补充
动力学仿真动画 可分为下面几种行为
一. 重力行为(UIGravityBehavior)
1.注意在给控价添加仿真动画时分以下几个步骤
>1.创建仿真者
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
>2.创建仿真行为对象
UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[ self.redView]];
>3.把仿真行为对象添加到仿真者中
[self.animator addBehavior:gravity];
2.重力行为对象几个属性
1>@property (readwrite, nonatomic) CGVector gravityDirection;
gravity.gravityDirection= CGVectorMake(10,10);
里面既有大小也有方向
2>@property (readwrite, nonatomic) CGFloat angle;
gravity.angle = M_PI_4;
设置重力的方向
3>@property (readwrite, nonatomic) CGFloat magnitude;
gravity.magnitude =10;
设置重力的大小
默认为1000point/senond^2;
二. 碰撞行为(UICollisionBehavior)
1.步骤
1>创建仿真者
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
2>创建仿真行为对象
UICollisionBehavior * collision = [[UICollisionBehavior alloc] initWithItems:@[ self.redView,self.blueView]];
3>把仿真行为对象添加到仿真者中
[self.animator addBehavior:collision];
2.碰撞行为对象几个属性
1>.转换边界 把整个view设置为碰撞边界
collision.translatesReferenceBoundsIntoBoundary = YES;
设置路径 把路径作为碰撞的边界
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 400, 200, 100)];
//给碰撞边界设置唯一标识 通过此标识可获取该边界
[collision addBoundaryWithIdentifier:@"word" forPath:path];
2>.碰撞模式
collision.collisionBehaviorMode=下面的枚举值
UICollisionBehaviorModeItems= 1 << 0, 元素与元素之间的碰撞
UICollisionBehaviorModeBoundaries= 1 << 1,元素与边界之间的碰撞
UICollisionBehaviorModeEverything= NSUIntegerMax
元素/边界 元素/元素 都碰撞
3>.碰撞代理
<UICollisionBehaviorDelegate> 遵守此协议
collision.collisionDelegate = self;
//碰撞行为的代理方法 这个是常用的代理方法 还有其他代理方法 可去头文件看;
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p{
//1.获取参与碰撞的元素
UIView * view = (UIView *) item;
//2.获取碰撞边界的唯一标示
NSString * ID = (NSString *) identifier;
}
三. 吸附行为(UISnapBehavior)
1.步骤
1>创建仿真者
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
2>创建仿真行为对象
UISnapBehavior * snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:locP];
3>把仿真行为对象添加到仿真者中
[self.animator addBehavior:snap];
2.吸附行为对象几个属性
1>@property (nonatomic, assign) CGPoint snapPoint
吸附到哪个点
2>@property (nonatomic, assign) CGFloat damping;
阻尼系数 0 最抖动 1 最不抖动
四. 附着行为(UIAttachmentBehavior)
1.步骤
1>创建仿真者
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
2>创建仿真行为对象
1> 元素附着到点
- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point;
UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:locP];
// - (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;
2> //元素之间的附着
UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToItem:self.blueView];
3>把仿真行为对象添加到仿真者中
[self.animator addBehavior:snap];
2.常用属性
@property (readwrite, nonatomic) CGPoint anchorPoint;
//设置附着点
@property (readwrite, nonatomic) CGFloat length;
设置点与元素 元素与元素之间附着的长度
@property (readwrite, nonatomic) CGFloat damping
阻尼系数 0最抖 1最不抖动
五. 推行为(UIPushBehavior)
1.步骤
1>创建仿真者
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
2>创建仿真行为对象
UIPushBehavior * push = [[UIPushBehavior alloc] initWithItems:@[self.redView] mode:UIPushBehaviorModeInstantaneous];
3>把仿真行为对象添加到仿真者中
[self.animator addBehavior:push];
2.属性
1>@property (nonatomic, readonly) UIPushBehaviorMode mode;
UIPushBehaviorModeContinuous 持续推动
UIPushBehaviorModeInstantaneous 瞬时
2>@property (nonatomic, readwrite) BOOL active;
//是否激活
3>@property (readwrite, nonatomic) CGFloat angle;
设置推动方向
4>@property (readwrite, nonatomic) CGFloat magnitude;
设置推力大小
5>@property (readwrite, nonatomic) CGVector pushDirection;
既有大小又有方向