iOS动画和特效(二)UIKit力学行为

UIKit Dynamics是UIKit的动力交互体系,比如重力,铰链连接,碰撞,悬挂等物理效果,它将2D物理引擎引入了人UIKit,它能使原本的动画和交互效果更加符合物理规律,当然动画效果也更逼真。

 view添加重力效果

  UIView *aView = [UIView new];
    aView.backgroundColor = [UIColor redColor];
    aView.frame = CGRectMake(self.view.frame.size.width/2, 100, 100, 100);
    
    [self.view addSubview:aView];
    
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[aView]];
    
    [animator addBehavior:gravityBehavior];
    
    self.animator = animator;

UIKit Dynamics基础

为了实现动力行为UI,我们必须先了解四个对象

  • UIDynamicItem:用来描述一个力学物体的状态,其实就是实现了UIDynamicItem委托的对象,或者抽象为有面积有旋转的质点;
  • UIDynamicBehavior:动力行为的描述,如例子中的重力行为
  • UIDynamicAnimator:动力行为(UIDynamicBehavior)的容器,添加到容器内的行为将发挥作用。
  • ReferenceView:力学参考对象,就是对象在哪个view中会产生响应的力学行为,例子中

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

    对象产生力学作用的区域是 self.view

场见的力学行为有这么几种

  • UIAttachmentBehavior:连接行为
  • UICollisionBehavior:碰撞行为
  • UIGravityBehavior:重力行为
  • UIPushBehavior:推力行为
  • UISnapBehavior:不知道怎么翻译,实际效果类似吸引力

重力+碰撞

前面的demo中view会一直落下,变成落到地下后就停止

    UIView *aView = [UIView new];
    aView.backgroundColor = [UIColor redColor];
    aView.frame = CGRectMake(self.view.frame.size.width/2, 100, 100, 100);
    aView.transform = CGAffineTransformRotate(aView.transform, 45);

    [self.view addSubview:aView];
    
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[aView]];
    
    [animator addBehavior:gravityBehavior];
    
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[aView]];
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    
    [animator addBehavior:collisionBehavior];
    collisionBehavior.collisionDelegate =self;
    self.animator = animator;

重力+附着力

UIAttachmentBehavior是连接行为,要把view钉在某一位置,就给他添加连接行为即可

UIView *aView = [UIView new];
    aView.backgroundColor = [UIColor redColor];
    aView.frame = CGRectMake(self.view.frame.size.width/2, 100, 100, 100);
    aView.transform = CGAffineTransformRotate(aView.transform, 45);

    [self.view addSubview:aView];
    
    self.aView = aView;
    
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    
    UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]initWithItems:@[aView]];
    
    [animator addBehavior:gravityBehavior];
    
    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]initWithItems:@[aView]];
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    
    [animator addBehavior:collisionBehavior];
    collisionBehavior.collisionDelegate =self;
    
    
    UIPanGestureRecognizer *gest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleAttachmentGesture:)];
    
    [aView addGestureRecognizer:gest];
    
    self.animator = animator;

- (void)handleAttachmentGesture:(UIPanGestureRecognizer*)gesture{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        CGPoint squareCenterPoint = CGPointMake(self.aView.center.x, self.aView.center.y-100);
        UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc]initWithItem:self.aView attachedToAnchor:squareCenterPoint];
        self.attachmentBehavior = attachmentBehavior;
        [self.animator addBehavior:attachmentBehavior];
    }else if (gesture.state==UIGestureRecognizerStateChanged){
        [self.attachmentBehavior setAnchorPoint:[gesture locationInView:self.view]];
    }else if (gesture.state==UIGestureRecognizerStateEnded){
        [self.animator removeBehavior:self.attachmentBehavior];
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值