点击事件不会在动画图像视图上触发
Posted
技术标签:
【中文标题】点击事件不会在动画图像视图上触发【英文标题】:tap event does not fire on animated imageview 【发布时间】:2019-05-02 09:29:34 【问题描述】:我想要一个带有UIBezierPath
(从上到下滑动)的动画图像视图被点击。但是我发现的每一个谷歌帮助都没有解决它。但它不会打印“点击”。
动画效果很好。也许有人有线索?
非常感谢您的帮助。
override func viewDidLoad()
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "advent_button"))
let dimension = 25 + drand48() * 30
imageView.frame = CGRect(x: 0, y: 0, width: dimension, height: dimension)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.isUserInteractionEnabled = true
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = customPath().cgPath
animation.duration = 20
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
animation.repeatCount = 0.5
animation.timingFunction = CAMediaTimingFunction(name: .easeOut)
imageView.layer.add(animation, forKey: nil)
view.addSubview(imageView)
@objc func handleImageTap()
print ("clicked")
func customPath() -> UIBezierPath
let path = UIBezierPath()
path.move(to: CGPoint(x: 200, y: 0))
let endpoint = CGPoint(x: 20 , y: 700)
let cp1 = CGPoint(x: 90, y: 250)
let cp2 = CGPoint(x: 180, y: 400)
path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
return path
【问题讨论】:
为什么不使用 uibutton 而不是 uiimageview。则无需添加手势 你试过了吗?使用 NSTimer ***.com/questions/12287267/… 使用按钮的行为相同。 【参考方案1】:我尝试了以下方法:
UIView.animate(withDuration: 30, delay: delay, options: [.allowUserInteraction], animations:
self.button.frame = CGRect(
x: randomXEndShift,
y: 700,
width: dimension,
height: dimension)
, completion: nil)
表现出相同的行为。即使使用.allowUserInteraction
也无济于事。
【讨论】:
【参考方案2】:我有你的问题。我确定您触摸的是层而不是UIImageView
,并且您已将uitapgesturerecognizer
添加到UIImageview
而不是层。做一件事
animation.isRemovedOnCompletion = true
并设置imageView.frame
动画停止的类似坐标
完整代码:
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "Icon123"))
imageView.backgroundColor = .red
// let dimension = 25 + drand48() * 30
imageView.isUserInteractionEnabled = true
imageView.frame = CGRect(x: 10, y: 200, width: 50, height: 50)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
imageView.addGestureRecognizer(tapGestureRecognizer)
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = customPath().cgPath
animation.duration = 5
animation.fillMode = .forwards
animation.isRemovedOnCompletion = true
animation.repeatCount = 0.5
animation.timingFunction = CAMediaTimingFunction(name: .easeOut)
imageView.layer.add(animation, forKey: nil)
view.addSubview(imageView)
// Do any additional setup after loading the view, typically from a nib.
@objc func handleImageTap()
print ("clicked")
func customPath() -> UIBezierPath
let path = UIBezierPath()
path.move(to: CGPoint(x: 200, y: 0))
let endpoint = CGPoint(x: 20 , y: 700)
let cp1 = CGPoint(x: 90, y: 250)
let cp2 = CGPoint(x: 180, y: 400)
path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
return path
【讨论】:
我不太清楚你使用相似坐标是什么意思? 我的意思是说,如果你想将你的图像视图设置在图层动画结束的位置。那么您必须使用 imageview.frame 属性或 imageview.center 在该位置设置框架 别忘了使用animation.isRemovedOnCompletion = true 如果我设置 .isRemovedOnCompletion = true 动画甚至不会开始 让我粘贴我的代码,我有编辑。我也应该上传 gif 吗?它对我来说很完美。你必须在动画完成后设置图像视图的框架以上是关于点击事件不会在动画图像视图上触发的主要内容,如果未能解决你的问题,请参考以下文章
触发图像点击事件时,如何将图像保存到手机内存(例如 SD 卡或本地硬盘驱动器)?