我有一个带有tap功能的视图(启用了用户交互)。如果我在不改变alpha的情况下进行设置就可以了。但是如果我尝试用延迟更改此视图的alpha,则在此动画期间无法点击它。有人可以帮忙吗?
这是我的代码:
import UIKit
class ViewController: UIViewController {
var myView = UIView()
var n = 0
override func viewDidLoad() {
super.viewDidLoad()
setView()
changeAlpha()
}
func changeAlpha() {
UIView.animate(withDuration: 5) {
self.myView.alpha = 0.1
}
}
func setView() {
let size = view.frame.width
myView = UIView(frame: CGRect(x: 0, y: 0, width: size/3, height: size/3))
myView.center = view.center
myView.backgroundColor = UIColor.red
view.addSubview(myView)
myView.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(gameObjectTapped(_:)))
myView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func gameObjectTapped(_ recognizer:UITapGestureRecognizer) {
print("# tap \(n)")
n+=1
}
答案 0 :(得分:1)
使用选项.allowUserInteraction
运行动画:
UIView.animate(withDuration: 5, delay: 0, options: [.allowUserInteraction], animations: {
self.myView.alpha = 0.1
}, completion: nil)
答案 1 :(得分:0)
改变你的
UIView.animate(withDuration: 5) {
self.myView.alpha = 0.1
}
到
UIView.animate(withDuration: 5, delay: 0.0, options: .allowUserInteraction, animations: {
self.myView.alpha = 0.1
}, completion: nil)