gpt4 book ai didi

swift - 点击事件不会在动画 ImageView 上触发

转载 作者:行者123 更新时间:2023-11-30 10:44:51 24 4
gpt4 key购买 nike

我想要一个带有 UIBezierPath (从上到下滑动)的动画 imageView 被单击。但我发现的每一个谷歌帮助都没有解决这个问题。但它不会打印“点击”。

动画效果非常完美。也许有人有线索?

非常感谢您的帮助。

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
}

最佳答案

我尝试过以下方法:

UIView.animate(withDuration: 30, delay: delay, options: [.allowUserInteraction], animations: {
self.button.frame = CGRect(
x: randomXEndShift,
y: 700,
width: dimension,
height: dimension)
}, completion: nil)

显示相同的行为。即使使用 .allowUserInteraction 也没有任何帮助。

关于swift - 点击事件不会在动画 ImageView 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com