UIKit動⼒力學最⼤大的特色是將現實世界動⼒力驅動的動畫引⼊入了UIKit, ⽐好比重⼒力,鉸鏈鏈接,碰撞,懸掛等效果,即將2D物理引擎引⼊入了 UIKitide
• 注意:UIKit動⼒力學的引⼊入,並非爲了替代CA或者UIView動畫,在 絕⼤大多數狀況下CA或者UIView動畫仍然是最優⽅方案,只有在須要引 ⼊入逼真的交互設計的時候,才須要使⽤用UIKit動⼒力學它是做爲現有交互 設計和實現的⼀一種補充動畫
動力學仿真動畫 可分爲下面幾種行爲atom
一. 重力行爲(UIGravityBehavior)spa
1.注意在給控價添加仿真動畫時分如下幾個步驟.net
>1.建立仿真者設計
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];代理
>2.建立仿真行爲對象orm
UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[ self.redView]];對象
>3.把仿真行爲對象添加到仿真者中get
[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;
既有大小又有方向