UIkit Dynamics

UIkit Dynamics是UIkit的一套基于现实世界物理驱动的一套交互体系,包含了我们常见的物理知识现象,如重力、碰撞、弹力等效果。而UIKit Dynamice提供了一整套逼真的物理动作,在实现一些逼真的物理效果的时事倍功半。

有四个概念我们来稍微描述一下:

  • UIDynamicItem 就是物理体系可以影响到的物体,像现实世界的桌子、椅子。在ios我们常见的UI控件像UIButton、UIView等实现了该接口。
  • UIDynamicBehavior 一个行为,像重力或者弹性、吸附等效果
  • UIDynamicAnimator 一个动画执行者,所有的行为都可以加入这个执行者,重力+碰撞就可以模拟高空坠落拉!
  • referenceView 参照体系,就像现实世界的参照物。

一、重力行为UIGravityBehavior

var animator : UIDynamicAnimator!
override func viewDidLoad() {
    super.viewDidLoad()
    //box
    let box : UIView = UIView(frame: CGRectMake(135, 80, 50, 50))
    box.backgroundColor = UIColor.grayColor()
    self.view.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(box)
    //参照物为背景view
    self.animator = UIDynamicAnimator(referenceView: self.view)
    //为box添加重力行为
    let gravity = UIGravityBehavior(items: [box])
    //执行动画
    self.animator.addBehavior(gravity)
}

这里注意把animator声明为类的成员变量,不然animator在viewDidLoad后就释放内存引用,就没有什么重力效果了。

GIF效果图

我们可以设置重力的向量,大小和方向就是坠落的方向和下坠加速度了。

gravity.gravityDirection = CGVector(dx: 0, dy: 5)

二、碰撞行为UICollisionBehavior

就这么无休止的坠落深渊未免也太无聊了吧,我们来让那个它跟屏幕下方进行碰撞。

var animator : UIDynamicAnimator!
override func viewDidLoad() {
    super.viewDidLoad()
    //box
    let box : UIView = UIView(frame: CGRectMake(135, 80, 50, 50))
    box.backgroundColor = UIColor.grayColor()
    self.view.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(box)
    //参照物为背景view
    self.animator = UIDynamicAnimator(referenceView: self.view)
    //为box添加重力行为
    let gravity = UIGravityBehavior(items: [box])
    //执行动画
    self.animator.addBehavior(gravity)
GIF效果图

三、连线行为UIAttachmentBehavior

连线就是两个物体之间进行连接或者一个物体跟一个锚点之间连接。

参数说明:UIAttachmentBehavior(item: offsetFromCenter: attachedToAnchor: )

  • item:物理体系可以影响到的物体

  • offsetFromCenter:物体中心偏移

  • attachedToAnchor:跟物体相连的锚点

      var animator : UIDynamicAnimator!
      override func viewDidLoad() {
             super.viewDidLoad()
      
            animator = UIDynamicAnimator(referenceView: self.view)
      
            //静止的box
            let staticBox = UIView(frame: CGRectMake(50, 50, 50, 50))
            staticBox.backgroundColor = UIColor.redColor()
            staticBox.layer.cornerRadius = 5 ;
           //下坠的box
           let dynamicBox = UIView(frame: CGRectMake(250, 50, 50, 50))
          dynamicBox.backgroundColor = UIColor.grayColor()
          dynamicBox.layer.cornerRadius = 5 ;
      
            //为dynamicBox添加重力行为
            let cravity = UIGravityBehavior(items: [dynamicBox])
          //为dynamicBox添加连线行为
           let attachment = UIAttachmentBehavior(item: dynamicBox, offsetFromCenter: UIOffsetMake(0, 0), attachedToAnchor: CGPointMake(staticBox.center.x, staticBox.center.y))
          //为dynamicBox添加碰撞行为
          let collision = UICollisionBehavior(items: [dynamicBox])
          collision.translatesReferenceBoundsIntoBoundary = true
      
          self.view.addSubview(staticBox)
          self.view.addSubview(dynamicBox)
      
          animator.addBehavior(cravity)
          animator.addBehavior(attachment)
          animator.addBehavior(collision) 
     }
    
GIF效果图

想让“绳子”有弹性加上这段:attachment.frequency = 1

四、吸附行为UISnapBehavior

很有趣的“牛皮糖”效果

var animator : UIDynamicAnimator!
let tapGesture = UITapGestureRecognizer()
var box:UIView!  
override func viewDidLoad() {
    super.viewDidLoad()
    
    //添加手势,轻触self.view,
    self.tapGesture.addTarget(self, action: "tappedView")
    self.view.userInteractionEnabled = true ;
    self.view.addGestureRecognizer(tapGesture)
    
    self.animator = UIDynamicAnimator(referenceView: self.view)
    
    // Do any additional setup after loading the view, typically from a nib.
    //物体
    self.box = UIView(frame: CGRectMake(110, 50, 100, 100))
    box.backgroundColor = UIColor.grayColor()
    box.layer.cornerRadius = 5
    
    self.view.addSubview(box)
}

//手势回调方法
func tappedView(){
    let point = self.tapGesture.locationInView(self.view)
    self.animator.removeAllBehaviors()
    let snap = UISnapBehavior(item: self.box, snapToPoint: point)
    self.animator.addBehavior(snap)
}
GIF效果图

四、推力UIPushBehavior

为物体施加一个外力,遵循力的合成

var box : UIView!
var animator : UIDynamicAnimator!
var push : UIPushBehavior!    
let tapGetsture = UITapGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()
    self.animator = UIDynamicAnimator(referenceView: self.view)
    
    //self.view加入轻触手势
    self.tapGetsture.addTarget(self, action: "tappedView")
    self.view.userInteractionEnabled = true
    self.view.addGestureRecognizer(tapGetsture)
    
    //box
    self.box = UIView(frame: CGRectMake(110, 234, 100, 100))
    self.box.layer.cornerRadius = 5 ;
    self.box.backgroundColor = UIColor.grayColor()

    //加入box
    self.view.addSubview(self.box)
    //碰撞
    let collision = UICollisionBehavior(items: [box])
     collision.setTranslatesReferenceBoundsIntoBoundaryWithInsets(UIEdgeInsets(top: self.topLayoutGuide.length, left: 0, bottom: self.bottomLayoutGuide.length, right: 0))
    //推力
    self.push = UIPushBehavior(items: [self.box], mode: UIPushBehaviorMode.Instantaneous)
    self.push.angle = 0
    self.push.magnitude = 0 ;
    
    //加入行为
    self.animator.addBehavior(self.push)
    self.animator.addBehavior(collision)
}

//轻触手势
func tappedView(){
    
    let tapPoint = self.tapGetsture.locationInView(self.view)
    let centerPoint = CGPointMake(self.box.center.x, self.box.center.y)
    
    //角度和大小
    let angle =  atan2(tapPoint.y - centerPoint.y, tapPoint.x - centerPoint.x)
    var distance = sqrtf(powf(Float(tapPoint.x - centerPoint.x),2.0) + powf(Float(tapPoint.y - centerPoint.y),2))
    distance =  min(distance, 100.00)
    
    self.push.setAngle(angle, magnitude: CGFloat(distance/100))
    
    //每次更改push属性时,push都会变为不可活动状态,需要手动更改active为true
    self.push.active = true ;
    }
GIF效果图

你可能感兴趣的:(UIkit Dynamics)