UIKit Dynamics 用來模擬物理動作我擂,有重力、碰撞缓艳、彈跳校摩、附著、瞬間位移郎任、推力秧耗、元素行為。下面分別是各個行為的效果圖舶治。
所有動畫行為都要添加到一個UIDynamicAnimator中分井,需要綁定一個引用視圖,動態(tài)行為的視圖必須是引用視圖的子視圖霉猛。
分別來講解
- 重力行為
- (void)startAnimation {
//animator必須是全局的尺锚,要不然沒有動畫效果
// UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.ballView]];
[gravityBehavior setGravityDirection:CGVectorMake(0.0f, 0.1f)];//重力方向向下,0.1表示以地球重力十分之一的力下落惜浅。
[animator addBehavior:gravityBehavior];
}
這里只設(shè)置了重力行為瘫辩,沒有設(shè)置碰撞行為,所以小球會一直下落至不見。
- 碰撞行為
- (void)startCollidesion {
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//重力行為
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.frogIV, self.dragonIV]];
gravityBehavior.gravityDirection = CGVectorMake(0.0, 0.1);
//碰撞行為
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.frogIV, self.dragonIV]];
collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
collisionBehavior.collisionDelegate = self;
[self.animator addBehavior:gravityBehavior];
[self.animator addBehavior:collisionBehavior];
}
#pragma mark - UICollisionBehaviorDelegate
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(nonnull id<UIDynamicItem>)item withBoundaryIdentifier:(nullable id<NSCopying>)identifier atPoint:(CGPoint)p {
if ([item isEqual:self.dragonIV]) {
self.dragonL.text = @"龍撞擊到了地面";
}
if ([item isEqual:self.frogIV]) {
self.frogL.text = @"青蛙撞擊到了地面";
}
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier {
NSLog(@"撞擊結(jié)束");
}
碰撞行為可以設(shè)置代理伐厌,可以在碰撞開始和結(jié)束時做想要的操作承绸。
- 附著行為
- (void)startAnimator {
//添加碰撞行為
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.frogIV, self.dragonIV]];
collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collisionBehavior];
//添加附著行為
CGPoint frogCenter = CGPointMake(self.frogIV.center.x, self.frogIV.center.y);
//讓龍的圖片隨著青蛙的中心點運動
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.dragonIV attachedToAnchor:frogCenter];
self.attachmentBehavior = attachmentBehavior;
[self.animator addBehavior:attachmentBehavior];
}
//讓青蛙圖片可以移動
- (IBAction)handleAttachmentGesture:(UIPanGestureRecognizer *)sender {
//將拖動的點設(shè)置為青蛙圖片的中心點
CGPoint panPoint = [sender locationInView:self.view];
self.frogIV.center = panPoint;
self.attachmentBehavior.anchorPoint = panPoint;
}
讓某個元素根據(jù)另一個元素的變化而變化。
- 彈跳行為
- (void)startAnimator {
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.animator = animator;
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.frogIV, self.dragonIV]];
collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.dragonIV]];
CGPoint frogCenter = CGPointMake(self.frogIV.center.x, self.frogIV.center.y);
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.dragonIV attachedToAnchor:frogCenter];
attachmentBehavior.frequency = 1.0f; //越大晃動越快
attachmentBehavior.damping = 0.1f; //越小晃動幅度越大
attachmentBehavior.length = 100.0f; //越大距離附著的點越遠
[animator addBehavior:collisionBehavior];
[animator addBehavior:gravityBehavior];
[animator addBehavior:attachmentBehavior];
}
其實就是附著行為的幾個屬性設(shè)置挣轨,frequency军熏、damping、length卷扮。
- 瞬間位移
- (IBAction)handleTapGesture:(UITapGestureRecognizer *)sender {
// 手指點到哪兒荡澎,就讓圖片瞬間移動哪兒
CGPoint panPoint = [sender locationInView:self.view];
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.animator = animator;
UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:self.frogIV snapToPoint:panPoint];
snapBehavior.damping = 0.5;
[self.animator addBehavior:snapBehavior];
}
讓某個元素瞬間移動到某個點
- 推力行為
- (void)startAnimator {
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.animator = animator;
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.dragonIV]];
collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[self.dragonIV] mode:UIPushBehaviorModeContinuous];
self.pushBehavior = pushBehavior;
pushBehavior.magnitude = 0.0;
pushBehavior.angle = 0.0;
[self.animator addBehavior:collisionBehavior];
[self.animator addBehavior:pushBehavior];
}
- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
//根據(jù)移動的點計算推力的角度和距離、
if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint panPoint = [sender locationInView:self.view];
CGPoint originPoint = CGPointMake(CGRectGetMidX(self.dragonIV.bounds), CGRectGetMidY(self.dragonIV.bounds));
CGFloat angle = atan2(panPoint.y - originPoint.y, panPoint.x - originPoint.x);
CGFloat distance = sqrtf(powf(panPoint.x - originPoint.x, 2) + powf(panPoint.y - originPoint.y, 2));
distance = MIN(distance, 100);
self.pushBehavior.magnitude = distance/100;
self.pushBehavior.angle = angle;
self.pushBehavior.active = YES;
}
}
- 元素行為
- (void)startAnimator {
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.animator = animator;
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.dragonIV, self.frogIV]];
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.dragonIV, self.frogIV]];
collisionBehavior.collisionMode = UICollisionBehaviorModeBoundaries;
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
//只對青蛙圖片設(shè)置元素屬性晤锹,用龍圖片進行對比
UIDynamicItemBehavior *propertiesBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.frogIV]];
propertiesBehavior.elasticity = 1.0f;
propertiesBehavior.allowsRotation = NO;
propertiesBehavior.angularResistance = 0.0f;
propertiesBehavior.density = 3.0f;
propertiesBehavior.friction = 0.5f;
propertiesBehavior.resistance = 1.0f;
[self.animator addBehavior:gravityBehavior];
[self.animator addBehavior:collisionBehavior];
[self.animator addBehavior:propertiesBehavior];
}