UIDynamic是UIKit動(dòng)力模型,ios7開始的技術(shù),提供一個(gè)模擬真實(shí)世界中力學(xué)相關(guān)點(diǎn)動(dòng)畫和交互系統(tǒng),比如:重力、碰撞、吸附、摩擦力等。
使用步驟
1.創(chuàng)建物理仿真器 (同時(shí)設(shè)置物理仿真范圍)== 相當(dāng)于運(yùn)動(dòng)場(chǎng)
2.創(chuàng)建物理仿真行為(同時(shí)設(shè)置物理仿真元素)== 行為相當(dāng)于跑步、元素相當(dāng)于人
3.將行為添加到仿真器中 == 相當(dāng)于張三在運(yùn)動(dòng)場(chǎng)里跑步
仿真行為有下面幾種:
UIGravityBehavior 重力行為
UICollisionBehavior 碰撞行為
UIAttachmentBehavior 吸附行為
UIPushBehavior 推力行為
UISnapBehavior 捕捉行為、閃爍行為
UIAttachmentBehavior 附著行為
UIDynamicItemBehavior 摩擦力
demo:
@property (strong, nonatomic) UIView *redView;
@property (strong, nonatomic) UIDynamicAnimator *anim;
// 懶加載anim
- (UIDynamicAnimator *)anim {
if(!_anim) {
_anim = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
return _anim;
}
- (void)viewDidLoad {
self.redView = [[UIView alloc] initWithFrame:CGRectmake:(10,10,50,50)];
self.redView.backGroundColor = [UIColor redColor];
[self.view addSubViews:self.redView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 1.創(chuàng)建物理仿真器
// 2.創(chuàng)建物理仿真行為
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 3.將行為添加到仿真器中
[self.anim addBehavior:gravity ];
}