动力学仿真动画

UIKit动⼒力学最⼤大的特色是将现实世界动⼒力驱动的动画引⼊入了UIKit, ⽐好比重⼒力,铰链链接,碰撞,悬挂等效果,即将2D物理引擎引⼊入了 UIKitide

注意:UIKit动⼒力学的引⼊入,并非为了替代CA或者UIView动画,在 绝⼤大多数状况下CA或者UIView动画仍然是最优⽅方案,只有在须要引 ⼊入逼真的交互设计的时候,才须要使⽤用UIKit动⼒力学它是做为现有交互 设计和实现的⼀一种补充动画

动力学仿真动画 可分为下面几种行为atom

 

一. 重力行为(UIGravityBehaviorspa

1.注意在给控价添加仿真动画时分如下几个步骤.net

>1.建立仿真者设计

  self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];代理

>2.建立仿真行为对象orm

  UIGravityBehavior * gravity = [[UIGravityBehavior alloc] initWithItems:@[ self.redView]];对象

>3.把仿真行为对象添加到仿真者中get

  [self.animator addBehavior:gravity];

2.重力行为对象几个属性

 1>@property (readwrite, nonatomic) CGVector gravityDirection;

   gravity.gravityDirection= CGVectorMake(10,10);

 里面既有大小也有方向


 2>@property (readwrite, nonatomic) CGFloat angle;

gravity.angle =  M_PI_4;

设置重力的方向

 3>@property (readwrite, nonatomic) CGFloat magnitude;

gravity.magnitude =10;

设置重力的大小

默认为1000point/senond^2;


二. 碰撞行为(UICollisionBehavior)

1.步骤

    1>建立仿真者

  self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

   2>建立仿真行为对象

UICollisionBehavior * collision = [[UICollisionBehavior alloc] initWithItems:@[ self.redView,self.blueView]];

  3>把仿真行为对象添加到仿真者中

[self.animator addBehavior:collision];


2.碰撞行为对象几个属性

     1>.转换边界 把整个view设置为碰撞边界

    collision.translatesReferenceBoundsIntoBoundary = YES;

     设置路径 把路径做为碰撞的边界

UIBezierPath * path = [UIBezierPath      bezierPathWithOvalInRect:CGRectMake(0, 400, 200, 100)];

    //给碰撞边界设置惟一标识  经过此标识可获取该边界

    [collision addBoundaryWithIdentifier:@"word" forPath:path];

  2>.碰撞模式

    collision.collisionBehaviorMode=下面的枚举值


    UICollisionBehaviorModeItems= 1 << 0, 元素与元素之间的碰撞


    UICollisionBehaviorModeBoundaries= 1 << 1,元素与边界之间的碰撞


    UICollisionBehaviorModeEverything= NSUIntegerMax

               元素/边界 元素/元素 都碰撞

     

     3>.碰撞代理

  <UICollisionBehaviorDelegate> 遵照此协议

      collision.collisionDelegate = self;

//碰撞行为的代理方法  这个是经常使用的代理方法   还有其余代理方法 可去头文件看;

- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p{

//1.获取参与碰撞的元素
   
UIView * view = (UIView *) item;
   
   
//2.获取碰撞边界的惟一标示

    NSString * ID = (NSString *) identifier;

}

三. 吸附行为(UISnapBehavior)

1.步骤

1>建立仿真者

  self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

2>建立仿真行为对象

 UISnapBehavior * snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:locP];

3>把仿真行为对象添加到仿真者中

[self.animator addBehavior:snap];

2.吸附行为对象几个属性

 1>@property (nonatomic, assign) CGPoint snapPoint

    吸附到哪一个点

 2>@property (nonatomic, assign) CGFloat damping; 

     阻尼系数 0 最抖动  1 最不抖动


四. 附着行为(UIAttachmentBehavior)

1.步骤

1>建立仿真者

  self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

2>建立仿真行为对象

  •   1> 元素附着到点 

- (instancetype)initWithItem:(id <UIDynamicItem>)item attachedToAnchor:(CGPoint)point; 

   UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToAnchor:locP];

//    - (instancetype)initWithItem:(id <UIDynamicItem>)item1 attachedToItem:(id <UIDynamicItem>)item2;

  •  2> //元素之间的附着

    UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc] initWithItem:self.redView attachedToItem:self.blueView];

3>把仿真行为对象添加到仿真者中

[self.animator addBehavior:snap];


2.经常使用属性

@property (readwrite, nonatomic) CGPoint anchorPoint;

//设置附着点

@property (readwrite, nonatomic) CGFloat length;

设置点与元素 元素与元素之间附着的长度

@property (readwrite, nonatomic) CGFloat damping

阻尼系数 0最抖  1最不抖动


五. 推行为(UIPushBehavior)

1.步骤

1>建立仿真者


  self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

2>建立仿真行为对象

UIPushBehavior * push = [[UIPushBehavior alloc] initWithItems:@[self.redView] mode:UIPushBehaviorModeInstantaneous];


3>把仿真行为对象添加到仿真者中

[self.animator addBehavior:push];

 

2.属性


1>@property (nonatomic, readonly) UIPushBehaviorMode mode;


   UIPushBehaviorModeContinuous 持续推进

       UIPushBehaviorModeInstantaneous   瞬时


2>@property (nonatomic, readwrite) BOOL active;

     //是否激活


3>@property (readwrite, nonatomic) CGFloat angle;

设置推进方向

4>@property (readwrite, nonatomic) CGFloat magnitude;

设置推力大小

5>@property (readwrite, nonatomic) CGVector pushDirection;

既有大小又有方向

相关文章
相关标签/搜索