对于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方法)。
如何在每次图像查看的基础上准确地检测出一个抽头?由于自定义路径动画需求,我不能使用UIView.animate或其他方法,而且我也不能创建覆盖透明的UIView来覆盖父视图,因为我需要这些“浮动器”来不吞咽这些事件。
发布于 2017-04-28 09:17:16
我认为您可以检查属于imageView或不属于onTap函数的抽头位置。如下所示:
func ontap(gesture:UITapGestureRecognizer) {
let point = gesture.location(in: parentView)
if imageView.layer.frame.contains(point) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(point)")
}
}https://stackoverflow.com/questions/43651351
复制相似问题