使用UIDynamicBehavior将Uneiewia添加到UIView Pan

时间:2016-08-01 20:20:47

标签: ios xcode uipangesturerecognizer uidynamicbehavior uidynamicanimator

我正在试图阻止' blockView'使用UIPanGestureRecognizer。当平底锅结束时,想要' blockView'用惯性减速,就像UIScrollView以惯性结束滚动一样。

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()
    self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view)

    let collider = UICollisionBehavior(items: [self.magnifiedView!])
    collider.collisionDelegate = self
    collider.collisionMode = .Boundaries
    collider.translatesReferenceBoundsIntoBoundary = true
    self.dynamicAnimator.addBehavior(collider)

    self.dynamicProperties = UIDynamicItemBehavior(items: [self.magnifiedView!])
    //self.dynamicProperties.elasticity = 1.0
    //self.dynamicProperties.friction = 0.0
    self.dynamicProperties.allowsRotation = false
    //self.dynamicProperties.resistance = 10.0
    self.dynamicAnimator.addBehavior(self.dynamicProperties)

}


func panBlockView (panGesture: UIPanGestureRecognizer) {
    switch panGesture {
    case .Began:
        self.blockViewLocation = (self.blockView.center)!
    case .Changed:
        let translation = panGesture.translationInView(self.view)
        self.blockView.center = CGPointMake(self.blockViewLocation.x + translation.x, self.self.blockViewLocation.y + translation.y)
    case .Ended, .Cancelled:
        self.dynamicAnimator.addLinearVelocity(panGesture.velocityInView(self.view), forItem: self.blockView)
    }
}

问题: 当我平移视图时,' blockView'试图捕捉到平底锅起源的原点。当平底锅结束时,它会产生惯性,但它从平底锅的原点开始(在捕捉到平移原点之后)。

注意: 上面的代码只对PAN THE VIEW有效。

1 个答案:

答案 0 :(得分:0)

添加Snap行为并快速解决问题。