我在 Swift 3 项目的 UIImageView 项目上使用自定义路径动画。代码概要如下:
// parentView and other parameters are configured externally
let imageView = UIImageView(image: image)
imageView.isUserInteractionEnabled = true
let gr = UITapGestureRecognizer(target: self, action: #selector(onTap(gesture:)))
parentView.addGestureRecognizer(gr)
parentView.addSubview(imageView)
// Then I set up animation, including:
let animation = CAKeyframeAnimation(keyPath: "position")
// .... eventually ....
imageView.layer.add(animation, forKey: nil)
onTap 方法以标准方式声明:
func onTap(gesture:UITapGestureRecognizer) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: FloatingImageHandler.parentView))")
}
问题是每次我调用 addGestureRecognizer 时,之前的手势识别器都会被覆盖,所以任何检测到的点击总是指向最后添加的图像,并且无法准确检测到位置(所以如果有人在 parentView 的任何地方点击,它仍然会触发 onTap 方法)。
如何在每个 ImageView 的基础上准确检测点击?由于自定义路径动画要求,我无法使用 UIView.animate 或其他方法,而且我也无法创建覆盖透明 UIView 来覆盖父 View ,因为我需要这些“ float 对象”来避免吞噬事件。
最佳答案
目前还不清楚您要实现什么目标,但我认为您应该将手势识别器添加到 imageView 而不是 parentView。
所以这样:
parentView.addGestureRecognizer(gr)
应替换为:
imageView.addGestureRecognizer(gr)
在你的 onTap 函数中你可能应该做这样的事情:
print("ImageView frame is \(gesture.view.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: gesture.view))")
关于ios - 检测动画 UIImageView 上的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43651351/