示例应用:
注意:
当前,UIKit Dynamics 不太实用于启动了自动布局的view,在这些问题得到解决前,建议对要实用的UIKit Dynamics代码做详尽的测试
UIKit Dynamics简介:
iOS7新增了UIKit Dynamics,是一组类和方法
实现UIKit Dynamics:
1.重力:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *forgiImageViiew;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//力学动画是生成器,每个动学动画生成器(UIDynamicAnimator)都是独立的,多个力学动画生成器可以同时运行。要让力学动画生成器持续运行
//必须有指向他的有效引用。相关的物体都处于静止状态后,力学动画生成器将暂停--不在执行任何动画,但对于未用的力学动画生成器,推荐的做法是将其删除
self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];//必须全局的参数
// 动态物体必须是参考视图(self.view)的子视图,否则力学动画生成器将不会生成任何动态效果
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[self.forgiImageViiew]];
[gravityBehavior setGravityDirection:CGVectorMake(0.0f, 0.1f)]; //property changed after printing of the book
[self.animator addBehavior:gravityBehavior];
}
2.碰撞:
<UICollisionBehaviorDelegate>
{
IBOutlet UIImageView *dragonImageView;
IBOutlet UIImageView *frogImageView;
IBOutlet UILabel *collisionOneLabel;
IBOutlet UILabel *collisionTwoLabel;
UIDynamicAnimator* animator;
}
- (void)viewDidLoad
{
[super viewDidLoad];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
// 重力行为
UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[frogImageView, dragonImageView]];
[gravityBehavior setGravityDirection:CGVectorMake(0.0f, 0.1f)]; //property changed after printing of the book
// 碰撞行为
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[frogImageView, dragonImageView]];
[collisionBehavior setCollisionMode: UICollisionBehaviorModeEverything];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:gravityBehavior];
[animator addBehavior:collisionBehavior];
collisionBehavior.collisionDelegate = self;
}
//指出了碰撞边界
//碰撞开始时调用
-(void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
{
if([item isEqual:frogImageView])
collisionOneLabel.text = @"Frog Collided";
if([item isEqual:dragonImageView])
collisionTwoLabel.text = @"Dragon Collided";
}
//碰撞结束时调用
//不指出碰撞边界
-(void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier
{ //collision has ended contact
}
//指出了碰撞边界
//碰撞开始时调用
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p{}
//碰撞结束时调用
//不指出碰撞边界
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2{}
3.连接:
{
IBOutlet UIImageView *frogImageView;
IBOutlet UIImageView *dragonImageView;
UIDynamicAnimator* animator;
}
//连接
@property(nonatomic, strong) UIAttachmentBehavior* attachmentBehavior;
- (void)viewDidLoad
{
[super viewDidLoad];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[dragonImageView, frogImageView]];
CGPoint frogCenter = CGPointMake(frogImageView.center.x, frogImageView.center.y);
self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:dragonImageView attachedToAnchor:frogCenter];
[collisionBehavior setCollisionMode: UICollisionBehaviorModeBoundaries];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:collisionBehavior];
[animator addBehavior:self.attachmentBehavior];
}
-(IBAction)handleAttachmentGesture:(UIPanGestureRecognizer*)gesture
{
CGPoint gesturePoint = [gesture locationInView:self.view];
frogImageView.center = gesturePoint;
[self.attachmentBehavior setAnchorPoint:gesturePoint];
}
4.弹簧效果:
{
IBOutlet UIImageView *frogImageView;
IBOutlet UIImageView *dragonImageView;
UIDynamicAnimator* animator;
}
@property(nonatomic, strong) UIAttachmentBehavior* attachmentBehavior;
- (void)viewDidLoad
{
[super viewDidLoad];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[dragonImageView, frogImageView]];
UIGravityBehavior* gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[dragonImageView]];
CGPoint frogCenter = CGPointMake(frogImageView.center.x, frogImageView.center.y);
self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:dragonImageView attachedToAnchor:frogCenter];
[self.attachmentBehavior setFrequency:1.0f];//设置振动频率
[self.attachmentBehavior setDamping:1.0f];//熨平动画的峰值
[self.attachmentBehavior setLength: 100.0f];//连接的长度
[collisionBehavior setCollisionMode: UICollisionBehaviorModeBoundaries];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:gravityBeahvior];
[animator addBehavior:collisionBehavior];
[animator addBehavior:self.attachmentBehavior];
}
-(IBAction)handleAttachmentGesture:(UIPanGestureRecognizer*)gesture
{
CGPoint gesturePoint = [gesture locationInView:self.view];
[self.attachmentBehavior setAnchorPoint:gesturePoint];
frogImageView.center = gesturePoint;
}
5.吸附:
{
IBOutlet UIImageView *frogImageView;
UIDynamicAnimator *animator;
}
//吸附
-(IBAction)handleSnapGesture:(UITapGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:self.view];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UISnapBehavior* snapBehavior = [[UISnapBehavior alloc] initWithItem:frogImageView snapToPoint:point];
snapBehavior.damping = 0.75f;
[animator addBehavior:snapBehavior];
}
6.推力:
{
IBOutlet UIImageView *dragonImageView;
UIDynamicAnimator* animator;
}
//推力
@property (nonatomic, strong) UIPushBehavior* pushBehavior;
- (void)viewDidLoad
{
[super viewDidLoad];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[dragonImageView]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[animator addBehavior:collisionBehavior];
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[dragonImageView] mode:UIPushBehaviorModeContinuous];
pushBehavior.angle = 0.0;
pushBehavior.magnitude = 0.0;
self.pushBehavior = pushBehavior;
[animator addBehavior:self.pushBehavior];
}
-(IBAction)handlePushGesture:(UITapGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:self.view];
CGPoint origin = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
CGFloat distance = sqrtf(powf(point.x-origin.x, 2.0)+powf(point.y-origin.y, 2.0));
CGFloat angle = atan2(point.y-origin.y,point.x-origin.x);
distance = MIN(distance, 100.0);
[self.pushBehavior setMagnitude:distance / 100.0];
[self.pushBehavior setAngle:angle];
[self.pushBehavior setActive:TRUE];
}
7.物体属性:
{
IBOutlet UIImageView *dragonImageView;
IBOutlet UIImageView *frogImageView;
UIDynamicAnimator* animator;
}
- (void)viewDidLoad
{
[super viewDidLoad];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[dragonImageView, frogImageView]];
UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[dragonImageView, frogImageView]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
UIDynamicItemBehavior* propertiesBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[frogImageView]];
propertiesBehavior.elasticity = 1.0f;
propertiesBehavior.allowsRotation = NO;//一个BOOL值,指定物体在受力时是否旋转,默认为YES
propertiesBehavior.angularResistance = 0.0f;//一个浮点数,取值范围0.0~cgFLOAT_MAX指定旋转阻力,其值越大,旋转速度下降的越快
propertiesBehavior.density = 3.0f;//物体的密度,默认情况下,100x100点的物体质量为1.0,100x200点的物体质量为2.0调整密度将影响重力和碰撞效果
propertiesBehavior.friction = 0.5f;//物体间的滑动阻力。0.0表示没有摩擦力,1.0表示摩擦力很大,但可将该属性设置为大于1.0的值,以进一步增大摩擦力
propertiesBehavior.resistance = 0.5f;//空气阻力,取值范围为0.0~CGFLOAT_MAX。0.0表示没有空气阻力,1.0表示一旦其他作用力消失,物体就会停止
[animator addBehavior:propertiesBehavior];
[animator addBehavior:gravityBehavior];
[animator addBehavior:collisionBehavior];
}
深入了解UIDynamicAnimator和UIDynamicAnimatorDelegate: