【发布时间】:2018-03-19 18:54:13
【问题描述】:
我无法触发动画,我将下面的函数放在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 函数之前和之后,但错误结果相同。请帮助为什么它告诉我当我清楚地在同一个视图控制器中时它发现为零。
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)
}
}
此外,我还尝试将 UIView.animate 移动到 viewdidload 中,但它根本没有动画,它只是立即改变颜色。
更新:我确实通过更改 Title 变量解决了发现的 nil 问题
var Title:UILabel! 转为var Title:UILabel! = UILabel()
但我仍然对动画有问题,即使我将 DURATION 延长到 5.0,它也没有使任何动画立即改变颜色
【问题讨论】:
-
您的
introViewController()中有什么? -
我上面有一个func,上面有一个uibutton和一个label name title。单击按钮时将更改背景颜色
-
问题是如何实例化
introViewController。因为如果您使用界面文件并且不加载它,则 ui 元素将是nil。 -
对不起,我的问题可能引起了误解。你的变量
introViewController是什么?你是如何实例化它的? -
我通过添加 var Title:UILabel 来添加标题标签!在顶部然后我将其添加到 view.addsubview 上的 viewdidload 就像上面的按钮一样