由上篇文章铁材,我們可知物理仿真行為有多種丢间,不過使用起來都大同小異,下面我就挑其中三種進(jìn)行介紹昌阿。
一、捕捉行為(UISnapBehavior)
可以讓物體迅速?zèng)_到某個(gè)位置(捕捉位置)恳邀,捕捉到位置之后會(huì)帶有一定的震動(dòng)懦冰。
1.UISnapBehavior的初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item
snapToPoint:(CGPoint)point;
2.UISnapBehavior常見屬性
// 用于減幅、減震(取值范圍是0.0 ~ 1.0谣沸,值越大刷钢,震動(dòng)幅度越小)
@property (nonatomic, assign) CGFloat damping;
3.UISnapBehavior使用注意
如果要進(jìn)行連續(xù)的捕捉行為乳附,需要先把前面的捕捉行為從物理仿真器中移除
4.代碼演示
- (void)test1 {
// 1.獲取當(dāng)前觸摸的手指
UITouch *touch = [touches anyObject];
// 2.更具手指取出位置
CGPoint point = [touch locationInView:touch.view];
// 吸附行為
// 1.創(chuàng)建物理仿真器
// 2.創(chuàng)建物理捕捉行為
UISnapBehavior *snapB = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point];
// 3.設(shè)置吸附行為的"減震"
snapB.damping = 0;
// 4.注意: 吸附行為默認(rèn)只能吸附一次, 如果多次吸附必須從仿真器中移除再重新添加
[self.anim removeAllBehaviors];
// 5.將物理仿真行為添加到仿真器中, self.dynamicAnimator為懶加載的物理仿真器對(duì)象
[self.dynamicAnimator addBehavior:snapB];
}
二内地、重力行為(UIGravityBehavior)
給定重力方向、加速度赋除,讓物體朝著重力方向掉落瓤鼻。
1.UIGravityBehavior的初始化
- (instancetype)initWithItems:(NSArray *)items;
item參數(shù) :里面存放著物理仿真元素
2.UIGravityBehavior常見方法
// 添加1個(gè)物理仿真元素
- (void)addItem:(id <UIDynamicItem>)item;
// 移除1個(gè)物理仿真元素
- (void)removeItem:(id <UIDynamicItem>)item;
- UIGravityBehavior常見屬性
// 添加到重力行為中的所有物理仿真元素
@property (nonatomic, readonly, copy) NSArray* items;
// 重力方向(是一個(gè)二維向量)
@property (readwrite, nonatomic) CGVector gravityDirection;
// 重力方向(是一個(gè)角度,以x軸正方向?yàn)?°贤重,順時(shí)針正數(shù)茬祷,逆時(shí)針負(fù)數(shù))
@property (readwrite, nonatomic) CGFloat angle;
// 量級(jí)(用來控制加速度,1.0代表加速度是1000 points /second2)
@property (readwrite, nonatomic) CGFloat magnitude;
4.代碼演練
- (void)test2 {
// 演示重力行為
// 1.創(chuàng)建物理仿真器
// 并且指定了當(dāng)前控制器的view作為仿真范圍
// UIDynamicAnimator *anim = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 2.創(chuàng)建物理仿真行為
// 并且指定紅色為作為仿真元素
UIGravityBehavior *gravityB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// 設(shè)置重力的方向
// gravityB.gravityDirection = CGVectorMake(1, 0);
// gravityB.gravityDirection = CGVectorMake(0, -1);
gravityB.gravityDirection = CGVectorMake(1, 1);
// 設(shè)置重力的角度
gravityB.angle = M_PI_2;
// 設(shè)置重力的加速度
gravityB.magnitude = 100.0;
// 將物理仿真行為添加到仿真器中, self.dynamicAnimator為懶加載的物理仿真器對(duì)象
[self.dynamicAnimator addBehavior:gravityB];
}
三并蝗、碰撞行為(UICollisionBehavior)
可以讓物體之間實(shí)現(xiàn)碰撞效果祭犯;可以通過添加邊界(boundary),讓物理碰撞局限在某個(gè)空間中滚停。
1沃粗、UICollisionBehavior常見的方法
- (void)addBoundaryWithIdentifier (id<NSCopying>)identifier
forPath:(UIBezierPath*)bezierPath;
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeAllBoundaries;
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;
// 設(shè)置參照視圖的bounds為邊界,并且設(shè)置內(nèi)邊距
-(void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;
2.UICollisionBehavior常見屬性
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
// 是否以參照視圖的bounds為邊界
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
// 碰撞模式(分為3種键畴,元素碰撞最盅、邊界碰撞、全體碰撞)
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
// 代理對(duì)象(可以監(jiān)聽元素的碰撞過程)
@property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;
注意:
碰撞行為要結(jié)合重力行為起惕,才有效果涡贱。
3.代碼演練
- (void)test3 {
// 碰撞
// 創(chuàng)建物理仿真器
// 創(chuàng)建物理仿真行為
UIGravityBehavior *gravigtyB = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
// gravigtyB.magnitude = 100;
// 創(chuàng)建碰撞仿真行為
UICollisionBehavior *collisionB = [[UICollisionBehavior alloc] initWithItems:@[self.redView, self.st]];
// 設(shè)置碰撞的邊界
// collisionB.translatesReferenceBoundsIntoBoundary = YES;
// 添加直線邊界
// [collisionB addBoundaryWithIdentifier:@"line" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(320, 420)];
// 添加圖形的邊界
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.view.frame];
[collisionB addBoundaryWithIdentifier:@"abc" forPath:path];
// 5.將物理仿真行為添加到仿真器中,self.dynamicAnimator為懶加載的物理仿真器對(duì)象
[self.dynamicAnimator addBehavior:gravigtyB];
[self.dynamicAnimator addBehavior:collisionB];
}