当我点击 scrollView
时,它会激活 UITapGestureRecognizer
,使 contentOffset
动画化约 2 秒。如何允许用户在拖动时中断动画并再次完全控制 scrollView
?现在,用户必须等到动画结束才能再次开始与 scrollView
交互。
注意:self
指的是scrollView
点击设置:
let singleTap = UITapGestureRecognizer(target: self, action: "subtleBounce:")
singleTap.cancelsTouchesInView = false
self.addGestureRecognizer(singleTap)
因此,当用户点击时:
func subtleBounce(gesture : UITapGestureRecognizer) {
let originalFrame = self.frame
UIView.animateWithDuration(0.1, delay: 0.0, usingSpringWithDamping: 1.5, initialSpringVelocity: 1.5, options: UIViewAnimationOptions.CurveLinear, animations: {
self.contentOffset.y -= 10.0
}, completion: {
Void in
UIView.animateWithDuration(0.6, delay: 0.0, usingSpringWithDamping: 0.1, initialSpringVelocity: 3.0, options: UIViewAnimationOptions.CurveLinear, animations: {
self.contentOffset.y += 10.0 }, completion: { Void in
})
})
}
所以上面的代码按预期工作。
这是我尝试停止动画并再次向用户提供 ScrollView 的控制权的方法:
let drag = UISwipeGestureRecognizer(target: self, action: "stopScrollAnimation:")
drag.cancelsTouchesInView = false
self.addGestureRecognizer(drag)
以及其他地方:
func stopScrollAnimation(gesture : UISwipeGestureRecognizer) {
self.layer.removeAllAnimations()
self.setContentOffset(CGPoint(x: 0.0, y: 0.0), animated: false)
}
但是这并没有按预期工作。动画仍然控制scrollView,用户无法与其交互。
如果您想了解我的意思,请查看 iOS7/8 锁屏。点击屏幕后,它还会开始播放动画。不过,用户可以控制 ScrollView 中间的动画。
编辑:接受 swift 或 obj-c 中的答案。
最佳答案
添加AllowUserInteraction到您的动画选项。
关于ios - 控制scrollView mid动画的contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27604965/