通过更改Alpha Swift iOS启用用户交互 [英] enable user interaction by changing alpha swift iOS
本文介绍了通过更改Alpha Swift iOS启用用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有点击功能的视图(已启用用户交互)。如果我在不更改alpha的情况下进行设置,则效果很好。但是,如果我尝试延迟更改此视图的Alpha,则在此动画期间无法点击它。有人可以帮忙吗?
I have a view with the tap function (user interaction is enabled). If I set it without changing alpha it works well. But if I try to change alpha of this view with delay, it cannot be tapped during this animation. Could somebody help?
这是我的代码:
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 <运行动画/ code>:
Run the animation with option .allowUserInteraction
:
UIView.animate(withDuration: 5, delay: 0, options: [.allowUserInteraction], animations: {
self.myView.alpha = 0.1
}, completion: nil)
这篇关于通过更改Alpha Swift iOS启用用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文