ios - 快速放置在函数内时无法触发 uiview.animate

标签 ios swift animation

我无法触发动画,我将下面的函数放在 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 函数之前和之后,但出现相同的错误结果。请帮助为什么它告诉我当我清楚地在同一个 View Controller 中时它发现为零。

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,它也不会立即改变颜色,也不会产生任何动画

最佳答案

问题在于 intro 是一个新初始化的 View Controller ,与有问题的实例完全无关。例如,不会在该新实例上调用 viewDidLoad。相反,您只想使用“self”,即加载的 View Controller 。

@objc func triggeranimation(){
    UIView.animate(withDuration: 0.5, animations: {
        //let intro:introViewController = introViewController()
        self.Title.backgroundColor = UIColor.blue
    }, completion: nil)
}

您的 viewDidLoad 代码中似乎也存在问题,您正在尝试初始化 Title 和 nextBtn 属性。相反,您不小心创建了同名的本地常量。这就是为什么你的属性(property)为零。

 // You have this now, which creates a local constant
 //let Title = UILabel(.. 

 // You want this instead, which initializes your property
 self.Title = UILabel(.. 

当然,对 nextBtn 执行同样的操作。

关于ios - 快速放置在函数内时无法触发 uiview.animate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49370404/

相关文章:

swift - 滑动动画以删除 UICollectionView 中的 Cell - Swift 2.0

html - 如何平滑地将 CSS 动画恢复到当前状态?

ios - 如何在 AFnetworking 中设置内容类型

ios - Swift NSNotificationCenter 不会触发

c# - 如何将泛型类型列表作为参数传递给 xamarin-C#-ios 中 TableSource 的构造函数?

swift - NSCollectionViewItem 从不实例化

swift - 如何在没有字典键的情况下获取值?

xcode - 即使删除所有支持文件 Xcode 6.4,图像文件仍然出现在应用程序中

android - ionic 状态栏问题 IOS

ios - Swift 3动画:代码运行但没有视觉效果