UIDynamic是用来模拟现实世界的物理模型,包括物理模型的
{
1.0 UIGravityBehavior 重力行为;
2.0 UICollisionBehavior 碰撞行为;
3.0 UIAttachmentBehavior 附着行为;
4.0 UISnapBehavior 吸附行为;
5.0 UIPushBehavior 推动行为;
}
// // ViewController.m // UIDynamicAnimator // // Created by Kong on 16/2/24. // // #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIDynamicAnimator *animator; @property (nonatomic, strong) UIView *animatiorView; @property (nonatomic, strong) UIView *animation2View; @property (nonatomic, assign) CGFloat damping; @property (strong, nonatomic) IBOutlet UIView *bothViews; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blackColor]; [self buildAnimatiorView]; [self setDynamicAnimator]; } - (void)buildAnimatiorView { self.animatiorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; self.animatiorView.backgroundColor = [UIColor purpleColor]; [self.bothViews addSubview:self.animatiorView]; self.animation2View = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)]; self.animation2View.backgroundColor= [UIColor magentaColor]; [self.bothViews addSubview:self.animation2View]; } - (void)setDynamicAnimator { self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.bothViews]; } - (IBAction)gravityBehavior:(id)sender { [self.animator removeAllBehaviors]; self.animatiorView.frame = CGRectMake(0, 0, 100, 100); self.animation2View.frame = CGRectMake(100, 300, 100, 100); /** UIGravityBehavior, 重力仿真。 */ UIGravityBehavior *snap = [[UIGravityBehavior alloc]\ initWithItems:@[self.animatiorView, self.animation2View]]; //这里设置哪个 View 进行重力仿真运动。 //防止重力碰撞。 UICollisionBehavior *collision = [[UICollisionBehavior alloc]\ initWithItems:@[self.animatiorView, self.animation2View]];//设置哪个 View 进行重力碰撞事件。 collision.translatesReferenceBoundsIntoBoundary = YES; //设置可以进行重力碰撞。 UIDynamicItemBehavior *itmeBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.animatiorView]]; itmeBehavior.elasticity = 0.5; //(弹性系数),值在【0~1】之间; itmeBehavior.friction = 20; //(摩擦系数); itmeBehavior.density = 20; //(密度);密度和 size 结合使用,计算物体的总质量; itmeBehavior.resistance = 1; //阻力,决定线性移动的阻力大小,与摩擦系数不同,摩擦系数值用于滑动; itmeBehavior.angularResistance = 1; //决定旋转运动时候的阻力大小,必须旋转的时候才能感受到; itmeBehavior.allowsRotation = YES; [self.animator removeAllBehaviors]; [self.animator addBehavior:snap]; [self.animator addBehavior:itmeBehavior]; [self.animator addBehavior:collision]; } - (IBAction)collisionBehavior:(id)sender { [self.animator removeAllBehaviors]; self.animatiorView.frame = CGRectMake(0, 0, 100, 100); self.animation2View.frame = CGRectMake(100, 300, 100, 100); UIGravityBehavior *snap = [[UIGravityBehavior alloc]\ initWithItems:@[self.animatiorView]]; //这里设置哪个 View 进行重力仿真运动。 /** * 这里就是反弹效果和其他效果一起使用的,重力作用 */ UICollisionBehavior *itmeBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.animatiorView]]; itmeBehavior.translatesReferenceBoundsIntoBoundary = YES; [self.animator removeAllBehaviors]; [self.animator addBehavior:snap]; [self.animator addBehavior:itmeBehavior]; } - (IBAction)attachmentBehavior:(id)sender { [self.animator removeAllBehaviors]; self.animatiorView.frame = CGRectMake(0, 0, 100, 100); self.animation2View.frame = CGRectMake(100, 300, 100, 100); UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.animatiorView attachedToAnchor:CGPointMake(200, 200)]; attachmentBehavior.anchorPoint = CGPointMake(200, 100); attachmentBehavior.length = 100; attachmentBehavior.damping = 0.1; //阻尼; attachmentBehavior.frequency = 1; //频率; attachmentBehavior.frictionTorque = 1; //转动摩擦力; [self.animator removeAllBehaviors]; [self.animator addBehavior:attachmentBehavior]; } - (IBAction)snapBehavior:(id)sender { [self.animator removeAllBehaviors]; self.animatiorView.frame = CGRectMake(0, 0, 100, 100); self.animation2View.frame = CGRectMake(100, 300, 100, 100); UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.animatiorView snapToPoint:CGPointMake(200, 100)]; snap.damping = 0.2; //【0~1】之间,0轻,1重; [self.animator addBehavior:snap]; } - (IBAction)PushBehavior:(id)sender { [self.animator removeAllBehaviors]; self.animatiorView.frame = CGRectMake(0, 0, 100, 100); self.animation2View.frame = CGRectMake(100, 300, 100, 100); UIPushBehavior *pushBehavior = [[UIPushBehavior alloc]\ initWithItems:@[self.animatiorView] mode:UIPushBehaviorModeContinuous]; pushBehavior.active = YES; pushBehavior.angle = 1.0f; //推动的方向; pushBehavior.magnitude = 1.0f; //推力的大小; UICollisionBehavior *itmeBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.animatiorView]]; itmeBehavior.translatesReferenceBoundsIntoBoundary = YES; [self.animator removeAllBehaviors]; [self.animator addBehavior:itmeBehavior]; [self.animator addBehavior:pushBehavior]; } @end
浙公网安备 33010602011771号