我在Swift 3项目的UIImageView项目上使用自定义路径动画。代码大纲如下:在动画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时间,之前的手势识别被覆盖,所以任何检测到的tap总是指向LAST添加的图像,并且该位置未被准确检测到(所以如果有人在父视图上的任何地方点击,它仍会触发onTap方法)。
如何在每个图像视图基础上准确检测水龙头?由于自定义路径动画需求,我无法使用UIView.animate或其他方法,而且我也无法创建覆盖透明UIView来覆盖父视图,因为我需要这些“浮动广告”来不吞噬事件。
我实现了你的子类,但有时它不起作用:按钮没有注意到它被点击,但有时它确实看到它。这是我的问题:https://stackoverflow.com/questions/46391003/retrieve-tapped-uibutton-in-a-moving-uibezierpath –