我无法触发动画,我将函数放在 viewdidload
函数之外,并创建了一个自定义函数,当按下该按钮时将触发该函数 . 但它没有创建动画,而是给了我一个错误 . 这是错误
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
这是将要触发的功能
@objc func triggeranimation(){
UIView.animate(withDuration: 0.5, animations: {
let intro:introViewController = introViewController()
intro.Title.backgroundColor = UIColor.blue
}, completion: nil)
}
并且我在viewdidload中以编程方式添加了按钮,这将触发上面的功能 . 我尝试将自定义函数放在 viewdidload
函数之前和之后,并且具有相同的错误结果 . 请帮助为什么当我清楚地在同一个视图控制器中时它告诉我它发现了nil .
let nextBtn = UIButton(frame: CGRect(x:0, y: 0, width: 0, height: 0))
nextBtn.translatesAutoresizingMaskIntoConstraints = false
nextBtn.setTitle("Next", for: UIControlState.normal)
nextBtn.backgroundColor = UIColor.blue
nextBtn.addTarget(self, action: Selector("triggeranimation"), for: .touchUpInside)
nextBtn.setTitleColor(UIColor.white, for: .normal)
self.view.addSubview(nextBtn)
// width cant change for some unknown reason
nextBtn.widthAnchor.constraint(equalToConstant: 10)
nextBtn.heightAnchor.constraint(equalToConstant: 20)
nextBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
nextBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
完整的代码
class introViewController: UIViewController{
var Title:UILabel!
var nextBtn:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let Title = UILabel(frame: CGRect(x: self.view.frame.size.width / 2, y: (self.view.frame.size.height + 30), width: 50, height: 10))
Title.translatesAutoresizingMaskIntoConstraints = false
Title.text = "WELCOME"
Title.font = UIFont.systemFont(ofSize: 24, weight: .bold)
Title.textAlignment = .center
Title.textColor = UIColor.black
self.view.addSubview(Title)
Title.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -250).isActive = true
Title.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
let nextBtn = UIButton(frame: CGRect(x:0, y: 0, width: 0, height: 0))
nextBtn.translatesAutoresizingMaskIntoConstraints = false
nextBtn.setTitle("Next", for: UIControlState.normal)
nextBtn.backgroundColor = UIColor.blue
nextBtn.addTarget(self, action: Selector("triggeranimation"), for: .touchUpInside)
nextBtn.setTitleColor(UIColor.white, for: .normal)
self.view.addSubview(nextBtn)
// width cant change for some unknown reason
nextBtn.widthAnchor.constraint(equalToConstant: 10)
nextBtn.heightAnchor.constraint(equalToConstant: 20)
nextBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
nextBtn.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive = true
}
@objc func triggeranimation(){
UIView.animate(withDuration: 0.5, animations: {
let intro:introViewController = introViewController()
intro.Title.backgroundColor = UIColor.blue
}, completion: nil)
}
}
此外,我还尝试在viewdidload中移动UIView.animate,但它根本没有动画,它只是立即改变颜色 .
更新:我确实通过将 Headers 变量 var Title:UILabel!
更改为 var Title:UILabel! = UILabel()
来解决找到的无问题
但是我仍然有动画问题,即使我将DURATION设置为5.0,它也不会立即改变颜色 .
1 回答
问题是
intro
是一个新初始化的视图控制器,它与所讨论的实例完全无关 . 例如,不会在该新实例上调用viewDidLoad . 相反,您只想使用"self",这意味着加载了视图控制器 .您的viewDidLoad代码中似乎也存在问题,您尝试初始化Title和nextBtn属性 . 相反,您不小心创建了同名的本地常量 . 这就是你的房产为零的原因 .
当然,为nextBtn做同样的事情 .