UIKit力學(xué)是基于Box2D開源物理引擎
- UIGravityBehavior 重力
- UICollisionBehavior 碰撞午绳,可以碰撞檢測(cè)
- UIAttachmentBehavior 吸附,互相吸附。兩個(gè)物體之間的距離是剛性的
- UIPushBehavior 推呼寸,向某個(gè)方向運(yùn)動(dòng)
- UISnapBehavior 甩稳捆,朝某個(gè)目標(biāo)點(diǎn)甩出
- UIDynamicItemBehavior 行為限制火的。它含有的設(shè)置行為力學(xué)的參數(shù):
- density:密度:如果是一個(gè)100 * 100的物體壶愤,密度為1.0,作用力是1.0馏鹤,加速度是100點(diǎn)/s2
- elasticity:彈力系數(shù)征椒,取值范圍是0-1,0表示沒有反彈湃累,1.0表示完全彈性碰撞
- friction:摩擦系數(shù)勃救, 0表示沒有摩擦力,1.0表示摩擦力很強(qiáng)脱茉,需要摩擦力很大可以大于1.0
- resistance:阻力剪芥,物體運(yùn)動(dòng)的時(shí)候垄开,在線性方向的阻力琴许,0.0沒有阻力,CGFLOAT_MAX表示最大阻力
- allowsRotation:是否允許旋轉(zhuǎn)
- angularResistance:角阻力:物體旋轉(zhuǎn)的時(shí)候溉躲,旋轉(zhuǎn)方向的阻力
用代碼說話
重力榜田、碰撞益兄、吸附、推
先看效果:
在SB中創(chuàng)建一個(gè)盒子箭券、一個(gè)小圓球净捅、一根木棍
Paste_Image.png
/**
* 盒子重力加速度下降
* 盒子在碰到棍子后彈起翻轉(zhuǎn)
* 盒子碰到球吸附
*/
@interface ViewController () <UICollisionBehaviorDelegate>{
BOOL _firstContact; // 記錄方塊是否是第一次碰撞
}
@property (weak, nonatomic) IBOutlet UIView *box; // 盒子
@property (weak, nonatomic) IBOutlet UIImageView *line; // 木棍
@property (weak, nonatomic) IBOutlet UIImageView *ball; // 球
@property (nonatomic, retain) UIDynamicAnimator * animator;
@property (nonatomic, retain) UIGravityBehavior * gravity; // 重力
@property (nonatomic, retain) UICollisionBehavior * collision; // 碰撞
@property (nonatomic, retain) UIAttachmentBehavior * attachment; // 吸附
@end
@implementation
ViewController
- (void)viewDidLoad {
_ball.layer.masksToBounds = YES;
_ball.layer.cornerRadius = _ball.frame.size.width / 2;
self.animator= [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 重力
self.gravity= [[UIGravityBehavior alloc] initWithItems:@[_box]];
// 設(shè)置重力的方向
// CGVector gravityDirection = {0.0, 5}; // 如果y為負(fù)向上運(yùn)動(dòng),值越大速度越大
// _gravity.gravityDirection = gravityDirection;
[_animator addBehavior:_gravity]; // 添加到animator中
// 碰撞
self.collision= [[UICollisionBehavior alloc] initWithItems:@[_box]];
_collision.translatesReferenceBoundsIntoBoundary= YES; // 檢測(cè)發(fā)生碰撞
[_animator addBehavior:_collision];
// 檢測(cè)是否與其他視圖邊界進(jìn)行碰撞
[_collision addBoundaryWithIdentifier:@"collision"fromPoint:_line.frame.origin toPoint:CGPointMake(_line.frame.origin.x+ _line.frame.size.width, _line.frame.origin.y)];
_collision.collisionDelegate = self; // 設(shè)置代理
// 參數(shù)
UIDynamicItemBehavior* item = [[UIDynamicItemBehavior alloc] initWithItems:@[_box]];
item.elasticity= 0.5;
[_animator addBehavior:item];
}
// 檢測(cè)到碰撞后進(jìn)行的處理
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id<UIDynamicItem>)item
withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p{
if( !_firstContact) {
_firstContact = YES;
// 吸附辩块,小球吸附在盒子里
self.attachment = [[UIAttachmentBehavior alloc] initWithItem:_ball attachedToItem:_box];
[self.animator addBehavior:_attachment];
// 推
UIPushBehavior * push
= [[UIPushBehavior alloc] initWithItems:@[_box]mode:UIPushBehaviorModeInstantaneous];
CGVector pushDir = {0.5, -0.5};
push.pushDirection =
pushDir;
push.magnitude = 5.0;
[_animator addBehavior:push];
}
}
@end
Demo在這里
甩
效果:
在SB為view添加一個(gè)點(diǎn)擊手勢(shì)
/**
* 盒子停在界面中蛔六,當(dāng)觸摸屏幕,盒子會(huì)先加速后減速到觸摸點(diǎn)
*/
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *box; // 盒子
@property (nonatomic, retain) UIDynamicAnimator * animator;
@property (nonatomic, retain) UISnapBehavior * snap; // 甩
@end
@implementation ViewController
- (void)viewDidLoad {
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (IBAction)handleSnapGesture:(UITapGestureRecognizer *)sender {
// 移除甩行為
[_animator removeBehavior:_snap];
// 添加甩行為
CGPoint point = [sender locationInView:self.view]; // 得到觸摸的點(diǎn)
self.snap = [[UISnapBehavior alloc] initWithItem:_box snapToPoint:point];
[_animator addBehavior:_snap];
}
Demo在這里