我正在尝试使用 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' 尝试捕捉到平移起源的原点。当平移“结束”时,它会产生惯性,但它从平移的原点开始(在捕捉到平移原点之后)。
注意: 上面的代码可以正常工作,只是为了 PAN THE VIEW。