UIDynamic中的三個重要概念數組
// 實例化一個重力行爲,並傳入須要作動力仿真的對象viewdom
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[view]];動畫
// 將重力行爲添加到animatorspa
[animator addBehavior:gravity];// 實例化碰撞行爲,並指定view參與碰撞代理
UICollisionBehavior *collsion = [[UICollisionBehavior alloc] initWithItems:@[view]];orm
// 指定是否啓用參照系的邊界對象
collsion.translatesReferenceBoundsIntoBoundary = YES;繼承
// 將碰撞行爲添加至animatorci
[animator addBehavior:collsion];rem
吸附行爲(Snap)
/ 刪除以前的吸附行爲
[_animator removeBehavior:_snap];
CGPoint location = [sender locationInView:self.view];
_snap = [[UISnapBehavior alloc] initWithItem:_boxImageView snapToPoint:location];
// 生成隨機振幅
CGFloat damping = arc4random_uniform(10) + 1;
_snap.damping = damping / 10.0f;
[_animator addBehavior:_snap];
if (UIGestureRecognizerStateBegan == sender.state) {
// 建立附加剛性行爲
CGPoint anchorPoint = CGPointMake(_boxImageView.center.x, _boxImageView.center.y);
_attachment = [[UIAttachmentBehavior alloc] initWithItem:_boxImageView offsetFromCenter:UIOffsetMake(-25.0, -25.0) attachedToAnchor:anchorPoint];
[_animator addBehavior:_attachment];
} else if (UIGestureRecognizerStateChanged == sender.state) {
// 設置行爲的錨點
[_attachment setAnchorPoint:[sender locationInView:self.view]];
} else if (UIGestureRecognizerStateEnded == sender.state) {
// 刪除附加行爲
[_animator removeBehavior:_attachment];
}
//附加彈性行爲
if (UIGestureRecognizerStateBegan == sender.state) {
CGPoint anchor = CGPointMake(_boxImageView.center.x, _boxImageView.center.y - 100);
_attachment = [[UIAttachmentBehavior alloc] initWithItem:_boxImageView attachedToAnchor:anchor];
[_animator addBehavior:_attachment];
[_attachment setFrequency:1.0f];
[_attachment setDamping:0.1f];
} else if (UIGestureRecognizerStateChanged == sender.state) {
[_attachment setAnchorPoint:[sender locationInView:self.view]];
} else if (UIGestureRecognizerStateEnded == sender.state) {
[_animator removeBehavior:_attachment];
}
// 計算兩點之間距離
CGFloat distance = sqrtf(powf(p.x - _firstPoint.x, 2.0) + powf(p.y - _firstPoint.y, 2.0));
CGFloat angle = atan2(p.y - _firstPoint.y, p.x - _firstPoint.x);
_push.magnitude = distance / 20;
_push.angle = angle;
[_push setActive:YES];