创建步骤
1.创建动画者对象,这必须是全局或者强指针指向的对象
2.创建动画,添加动画执行者views
3.设置属性等
4.将动画添加到动画者对象中,执行动画
//动画者对象需要创建在全局变量或者viewdidload中,不然会立即销毁
//操作动画
UIGravityBehavior *gra=[[UIGravityBehavior alloc]initWithItems:@[self.redView]];
//重力的方向
gra.angle = M_PI;
// gra.gravityDirection=CGVectorMake(0, 1);
//设置重力
gra.magnitude=1;
//在使用angle属性确定方向的时候,重力属性无论设置在哪里都有效果,但是
//如果使用magnitude属性时,重力属性的设置必须放在后面才有效果,否则无效
//让动画者执行某一个行为
[self.anim addBehavior:gra];
这个是碰撞的状态,
UICollisionBehaviorModeItems
仅仅是实体和实体直接的碰撞
UICollisionBehaviorModeBoundaries
仅仅是实体和边界之间的碰撞
UICollisionBehaviorModeEverything
所有碰撞
这是确定碰撞状态的方法
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
这是确定是否以参考系,应该是根layer的bounds为碰撞边界
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
UICollisionBehavior *coll=[[UICollisionBehavior alloc]initWithItems:@[self.redView,self.blueView]];
//设置参考view的bounds作为边界,默认为no
coll.translatesReferenceBoundsIntoBoundary=YES;
// UICollisionBehaviorModeItems
//item和item碰撞
// UICollisionBehaviorModeBoundaries
//item和边界
// UICollisionBehaviorModeEverything
//item和边界还有item都会碰撞
coll.collisionMode=UICollisionBehaviorModeEverything;
[self.anim addBehavior:coll];
添加碰撞的线,用线来创建边界
[coll addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(0, 200) toPoint:CGPointMake(200, 290)];
碰撞的4个代理方法
1.遵循协议
2.创建碰撞对象,设置代理
3.实现代理方法
- (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;
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;
//创建动画者对象
self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
//创建动画对象
UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.redView snapToPoint:p];
//这是振幅
snap.damping=0.8;
//所有dynamic都有的属性,监听,只要动画执行就一直调用
snap.action=^{
NSLog(@"123");
};
//添加动画到动画者对象中执行动画
[self.anim addBehavior:snap];
UIAttachmentBehavior 必须实现length才能起作用???
UITouch *t=touches.anyObject;
CGPoint p=[t locationInView:self.view];
//每次重新创建才能每次都移动,如果在viewDidLoad中创建,会导致点击不能改变位置
self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
UIAttachmentBehavior *att2=[[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToAnchor:p];
//必须设置这个长度附着到点的属性才能执行
att2.length=40;
[self.anim addBehavior:att2];
UITouch *t=touches.anyObject;
CGPoint p=[t locationInView:self.view];
//每次重新创建才能每次都移动,如果在viewDidLoad中创建,会导致点击不能改变位置
self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
//创建附着属性
UIAttachmentBehavior *att=[[UIAttachmentBehavior alloc]initWithItem:self.redView attachedToAnchor:p];
//必须设置这个长度附着到点的属性才能执行
att.length=20;
[self.anim addBehavior:att];
UITouch *t=touches.anyObject;
CGPoint p= [t locationInView:self.view];
self.anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
//=====
UIAttachmentBehavior *att=[[UIAttachmentBehavior alloc]initWithItem:self.redView offsetFromCenter:UIOffsetMake(0, 50) attachedToAnchor:p];
att.anchorPoint=p;
att.length=5;
[self.anim addBehavior:att];
//创建推
// UIPushBehaviorModeContinuous,
// UIPushBehaviorModeInstantaneous
UIPushBehavior *push=[[UIPushBehavior alloc]initWithItems:@[self.redView] mode:UIPushBehaviorModeContinuous];
CGFloat offX= p.x - self.view.center.x;
CGFloat offY= p.y - self.view.center.y;
push.pushDirection=CGVectorMake(-offX,-offY);
push.magnitude=1;
[self.anim addBehavior:push];
UIDynamicItemBehavior *itemB=[[UIDynamicItemBehavior alloc]initWithItems:@[self.redView]];
//这个是自身属性,弹性,取值0-1,越靠近0,弹性越小,越靠近1,弹性越大
itemB.elasticity=0.9;
//密度,默认是1;
itemB.density=1;
//摩擦力的值
itemB.friction=0;
思路分析
1.创建8个view,并且为8个view设置frame,让他们紧凑排列在一起,并且设置圆角
2.为每个view添加重力属性,碰撞属性,还有附着属性
3.这里需要一个数组来保存每个view,让前一个view和后一个view向附着
4.设置最后一个view的大小,以及他跟随手势的变化,添加手势
原文:http://www.cnblogs.com/jxiOSwiki/p/5233406.html