我在正在动画翻译的 UILabel 上有一个点击手势。每当您在动画期间点击标签时,点击手势都没有响应。
这是我的代码:
label.addGestureRecognizer(tapGesture)
label.userInteractionEnabled = true
label.transform = CGAffineTransformMakeTranslation(0, 0)
UIView.animateWithDuration(12, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
label.transform = CGAffineTransformMakeTranslation(0, 900)
}, completion: nil)
手势代码:
func setUpRecognizers() {
tapGesture = UITapGestureRecognizer(target: self, action: "onTap:")
}
func onTap(sender : AnyObject) {
print("Tapped")
}
有什么想法吗?谢谢:)
为 2021 年添加的注释:
现在这非常简单,您只需重写 hitTest。
最佳答案
如何在移动的 View 中检测触摸
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let pf = layer.presentation()!.frame
// note, that is in the space of our superview
let p = self.convert(point, to: superview!)
if pf.contains(p) { return self }
return nil
}
就这么简单
相关提示——
不要忘记,在大多数情况下,如果动画正在运行,您肯定会想要取消它。因此,假设有一个“移动目标”,并且您希望能够用手指捕获它并将其滑动到其他地方,自然地在这种用例中,您的 View Controller 中的代码看起来像..
func sliderTouched() {
if alreadyMoving {
yourPropertyAnimator?.stopAnimation(true)
yourPropertyAnimator = nil
}
etc ...
}
关于ios - 在动画 UIView 上点击手势不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998808/