ios - 检测动画 UIImageView 上的点击

标签 ios swift swift3 cakeyframeanimation

我在 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/

相关文章:

ios - UITextView 正确高亮单词

swift - 将 UnsafeMutablePointer 转换为 UnsafePointer

ios - 无法将变量传递给一个特定的类,实例成员不能用于类型错误

ios - CoreData导入和升级策略

ios - 使用 requestAccessToEntityType 时出现 BAD ACCESS 错误

Swift:如何检查反射中的泛型类型?

ios - 从 UIColor 中提取 RGB 值

iphone - 没有状态栏的截图

ios - 如何为 UITextView 中的每个段落放置不同的 headIndent(对齐方式)

ios - 如何在 iOS 中尝试/捕获此类错误