dynamic动力学

创建步骤

1.创建动画者对象,这必须是全局或者强指针指向的对象

2.创建动画,添加动画执行者views

3.设置属性等

4.将动画添加到动画者对象中,执行动画


1.重力

  1. //动画者对象需要创建在全局变量或者viewdidload中,不然会立即销毁
  2. //操作动画
  3. UIGravityBehavior *gra=[[UIGravityBehavior alloc]initWithItems:@[self.redView]];
  4. //重力的方向
  5. gra.angle = M_PI;
  6. // gra.gravityDirection=CGVectorMake(0, 1);
  7. //设置重力
  8. gra.magnitude=1;
  9. //在使用angle属性确定方向的时候,重力属性无论设置在哪里都有效果,但是
  10. //如果使用magnitude属性时,重力属性的设置必须放在后面才有效果,否则无效
  11. //让动画者执行某一个行为
  12. [self.anim addBehavior:gra];

2.碰撞

这个是碰撞的状态,

UICollisionBehaviorModeItems 
仅仅是实体和实体直接的碰撞

UICollisionBehaviorModeBoundaries 
仅仅是实体和边界之间的碰撞

UICollisionBehaviorModeEverything 
所有碰撞

这是确定碰撞状态的方法

@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;

这是确定是否以参考系,应该是根layer的bounds为碰撞边界

@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;

  1. UICollisionBehavior *coll=[[UICollisionBehavior alloc]initWithItems:@[self.redView,self.blueView]];
  2. //设置参考view的bounds作为边界,默认为no
  3. coll.translatesReferenceBoundsIntoBoundary=YES;
  4. // UICollisionBehaviorModeItems
  5. //item和item碰撞
  6. // UICollisionBehaviorModeBoundaries
  7. //item和边界
  8. // UICollisionBehaviorModeEverything
  9. //item和边界还有item都会碰撞
  10. coll.collisionMode=UICollisionBehaviorModeEverything;
  11. [self.anim addBehavior:coll];

3.碰撞边界以及代理

添加碰撞的线,用线来创建边界

  1. [coll addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(200, 290)];

碰撞的4个代理方法

1.遵循协议

2.创建碰撞对象,设置代理

3.实现代理方法

  1. - (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
  2. - (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;
  3. - (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;
  4. - (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;

4.甩行为

  1. //创建动画者对象
  2. self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
  3. //创建动画对象
  4. UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.redView snapToPoint:p];
  5. //这是振幅
  6. snap.damping=0.8;
  7. //所有dynamic都有的属性,监听,只要动画执行就一直调用
  8. snap.action=^{
  9. NSLog(@"123");
  10. };
  11. //添加动画到动画者对象中执行动画
  12. [self.anim addBehavior:snap];

5.附着行为

UIAttachmentBehavior 必须实现length才能起作用???

5.1.item与item之间

  1. UITouch *t=touches.anyObject;
  2. CGPoint p=[t locationInView:self.view];
  3. //每次重新创建才能每次都移动,如果在viewDidLoad中创建,会导致点击不能改变位置
  4. self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
  5. UIAttachmentBehavior *att2=[[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToAnchor:p];
  6. //必须设置这个长度附着到点的属性才能执行
  7. att2.length=40;
  8. [self.anim addBehavior:att2];

5.2.item与点之间

  1. UITouch *t=touches.anyObject;
  2. CGPoint p=[t locationInView:self.view];
  3. //每次重新创建才能每次都移动,如果在viewDidLoad中创建,会导致点击不能改变位置
  4. self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
  5. //创建附着属性
  6. UIAttachmentBehavior *att=[[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToAnchor:p];
  7. //必须设置这个长度附着到点的属性才能执行
  8. att.length=20;
  9. [self.anim addBehavior:att];

5.3.offset偏移

  1. UITouch *t=touches.anyObject;
  2. CGPoint p= [t locationInView:self.view];
  3. self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
  4. //=====
  5. UIAttachmentBehavior *att=[[UIAttachmentBehavior alloc]initWithItem:self.redView offsetFromCenter:UIOffsetMake(0, 50) attachedToAnchor:p];
  6. att.anchorPoint=p;
  7. att.length=5;
  8. [self.anim addBehavior:att];

5.4.弹性附着和刚性附着

6.推行为

  1. //创建推
  2. // UIPushBehaviorModeContinuous,
  3. // UIPushBehaviorModeInstantaneous
  4. UIPushBehavior *push=[[UIPushBehavior alloc]initWithItems:@[self.redView] mode:UIPushBehaviorModeContinuous];
  5. CGFloat offX= p.x - self.view.center.x;
  6. CGFloat offY= p.y - self.view.center.y;
  7. push.pushDirection=CGVectorMake(-offX,-offY);
  8. push.magnitude=1;
  9. [self.anim addBehavior:push];

7.动力学元素自身属性

  1. UIDynamicItemBehavior *itemB=[[UIDynamicItemBehavior alloc]initWithItems:@[self.redView]];
  2. //这个是自身属性,弹性,取值0-1,越靠近0,弹性越小,越靠近1,弹性越大
  3. itemB.elasticity=0.9;
  4. //密度,默认是1;
  5. itemB.density=1;
  6. //摩擦力的值
  7. itemB.friction=0;

8.练习(毛毛虫)

思路分析

1.创建8个view,并且为8个view设置frame,让他们紧凑排列在一起,并且设置圆角

2.为每个view添加重力属性,碰撞属性,还有附着属性

3.这里需要一个数组来保存每个view,让前一个view和后一个view向附着

4.设置最后一个view的大小,以及他跟随手势的变化,添加手势

posted @ 2016-03-02 00:12  jx猿星球  阅读(284)  评论(0编辑  收藏  举报