gpt4 book ai didi

ios - 通过更改 alpha swift iOS 启用用户交互

转载 作者:行者123 更新时间:2023-11-28 15:05:14 26 4
gpt4 key购买 nike

我有一个带有点击功能的 View (启用了用户交互)。如果我在不改变 alpha 的情况下设置它,它会很好地工作。但是,如果我尝试延迟更改此 View 的 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)

关于ios - 通过更改 alpha swift iOS 启用用户交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559553/

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