繁体   English   中英

使用UIDynamicBehavior将惯性添加到UIView Pan

[英]Adding Inertia to a UIView Pan with UIDynamicBehavior

我正在尝试使用UIPanGestureRecognizer平移'blockView'。 当平移“结束”时,希望“ blockView”以惯性减速,就像UIScrollView会以惯性结束滚动一样。

在此处输入图片说明

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”会尝试捕捉到平移所源自的原点。 平移“结束”时,它会产生惯性,但会从平移的原点开始(捕捉到平移的原点之后)。

注意:上面的代码没有问题,只是平移视图。

添加一个捕捉行为并对其进行捕捉可解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM