IOS菜鸟的所感所思(二)——UIKit中的UIDynamicAnimator

(一)介绍:iOS 7中引入了UIDynamic behavior 的概念,用于模拟物理世界中物体的一些物理效果。其中有UIGravityBehavior,UICollisionBehavior,UIPushBehavior,UIAttachmentBehavior,UISnapBehavior,

UIDynamicItemBehavior.


1.UIGravityBehavior(重力行为):UIGravityBehavior  *gravity=[[UIGravityBehavior alloc] initWithItems:@[self.squareView]];

         创建一个UIGravityBehavior类型的对象gravity,使得创建的squareView具有了重力行为。

UIDynamicAnimator(提供动力行为的上下文):self.animator=[[UIDynamicAnimator alloc]  initWithReferenceView:self.view];

          提供一个可以有动力行为的环境。
[self.animator addBehavior : gravity];将具有重力行为的的视图squareView放到self.view的环境中。这样我们创建的子视图在self.view中             就有了重力的行为。

2.UICollisionBehavior(碰撞行为):_ground = [[UICollisionBehavioralloc]initWithItems:@[self.squareView]];相当     于给视图squareView另一个属性碰撞的行为。接着设置其发生碰撞的边界是在哪里,

ground.translatesReferenceBoundsIntoBoundary =YES;

     其作用是将self.view的bounds的边界作为会碰撞的边界。这样子视图落下的时候在屏幕的底部就会发生碰撞。

3.UIPushBehavior(推动行为):self.pushBehavior = [[UIPushBehavior alloc]initWithItems:@[self.squareView]mode:UIPushBehaviorModeContinuous];

     子视图squareView有了可以连续推动的行为。

4.UIAttachmentBehavior(吸附行为):self.attachmentBehavior = [[UIAttachmentBehavior alloc]initWithItem:self.squareViewattachedToAnchor:self.anchorView.center];

      有四种初始化方法,这是其中的一种。

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

      其中第一个参数是指定对象具有attach这种行为,而第二个参数是指定锚点的位置。

5.UISnapBehavior(捕捉行为):CGPoint tapPoint = [paramTaplocationInView:self.view];

self.snapBehavior = [[UISnapBehavioralloc]

initWithItem:self.squareViewsnapToPoint:tapPoint];

      其作用是当点击屏幕上的一个点时,此时我们创建的子视图就会移动到相应的点去。

6.UIDynamicItemBehavior(可定义行为):UIDynamicItemBehavior *lessElasticity = [[UIDynamicItemBehavioralloc]initWithItems:@[self.topView]];

      lessElasticity.elasticity =1.0f;

      在UIDynamicItemBehavior中可以定义视图的属性,其中有allowsRotation,resistance,friction,elasticity,density.

当然这只是简单的介绍它们的行为,具体的还得进入到代码的世界才能对它们有比较全面的了解。


(二)下面做一个小的Demo,

1.创建一个SingleViewApplication的项目。

2.视图:将会用代码创建视图。

3.代码:

ViewController.m文件中:

//定义两个视图

@interfaceViewController ()

@property (nonatomic,strong)UIDynamicAnimator *animator;

@property (nonatomic,strong)UIView *greenView;

@property (nonatomic,strong)UIView *redView;


@end


//创建这两个视图

- (void) createView{

     _greenView= [self newViewWithCenter:CGPointMake(100.0f,0.0f) backgroundColor:[UIColor greenColor]];

    _redView = [self newViewWithCenter:CGPointMake(100.0f,50.0f) backgroundColor:[UIColor redColor]];

    

    [self.view addSubview:_greenView];

    [self.view addSubview:_redView];

}


//调用这个方法,初始化两个视图的颜色和中心点

- (UIView *) newViewWithCenter:(CGPoint)paramCenter backgroundColor:(UIColor *)paramBackgroundColor{

   UIView *newView = [[UIViewalloc] initWithFrame:CGRectMake(0.0f,0.0f, 50.0f,50.0f)];

    newView.backgroundColor = paramBackgroundColor;

    newView.center = paramCenter;

    

   return newView;

}

/**

 *  创建animator和视图的行为

 */

- (void)createAnimatorAndBehaviors{

    self.animator = [[UIDynamicAnimatoralloc] initWithReferenceView:self.view];

    

   UIGravityBehavior *gravityBehavior = [[UIGravityBehavioralloc] initWithItems:@[self.greenView,self.redView]];

    

    [self.animatoraddBehavior:gravityBehavior];

    

    UICollisionBehavior *collision = [[UICollisionBehavioralloc]initWithItems:@[self.greenView,self.redView]];

    collision.translatesReferenceBoundsIntoBoundary =YES;

    [self.animatoraddBehavior:collision];

}

/**

 *  创建视图会有的属性,即视图的发生碰撞时的弹力的大小

 */

- (void) createViewProperty{

   UIDynamicItemBehavior *moreElasticity= [[UIDynamicItemBehavioralloc]initWithItems:@[self.greenView]];

   moreElasticity.elasticity =1.0f;

    

   UIDynamicItemBehavior *lessElasticity = [[UIDynamicItemBehavioralloc]initWithItems:@[self.redView]];

    lessElasticity.elasticity =1.0f;

    

    [self.animatoraddBehavior:moreElasticity];

    [self.animatoraddBehavior:lessElasticity];

}

/**

 *  视图出现时会调用的方法

 *

 *  @param animated <#animated description#>

 */

- (void) viewDidAppear:(BOOL)animated{

    [superviewDidAppear:animated];

    

    [selfcreateView];

    [selfcreateAnimatorAndBehaviors];

    [selfcreateViewProperty];

}

这个小的Demo中包含了三种行为。如果想了解更多,请在网上查相关的资料。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值