通过更改 alpha swift iOS 启用用户交互
enable user interaction by changing alpha swift iOS
我有一个带有点击功能的视图(启用了用户交互)。如果我在不改变 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
}
运行 带选项的动画 .allowUserInteraction
:
UIView.animate(withDuration: 5, delay: 0, options: [.allowUserInteraction], animations: {
self.myView.alpha = 0.1
}, completion: nil)
改变你的
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)
我有一个带有点击功能的视图(启用了用户交互)。如果我在不改变 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
}
运行 带选项的动画 .allowUserInteraction
:
UIView.animate(withDuration: 5, delay: 0, options: [.allowUserInteraction], animations: {
self.myView.alpha = 0.1
}, completion: nil)
改变你的
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)