/**
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的短信清單,在快速滑動松手時,會有一個所有短信擠到一起,然後再逐漸張開的效果
*/
轉載于:https://www.cnblogs.com/DarbyCJ/p/3745608.html