UIView Animate仅在IBAction中进行动画制作。如何在我的情况下让它变为动画?

时间:2018-04-27 20:47:58

标签: ios swift

func pickWash() {

    bottomChange = self.mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -350.0)
    bottomChange.isActive = true

    UIView.animate(withDuration: 10.0, animations: {
        self.view.layoutIfNeeded()
        self.waterlessLabel.isHidden = false
        self.exteriorInterior.isHidden = false
        self.exteriorOnly.isHidden = false
        self.info.isHidden = false
        self.blackLine.isHidden = false
        self.extIntPrice.isHidden = false
        self.extPrice.isHidden = false
        self.confirmWash.isHidden = false
        self.when.isHidden = false
        self.timeChoice.isHidden = false

    }, completion: nil)

}

func tester(){
   self.pickWash()
}

实际上,我的代码中的测试方法是使用Google的自动填充功能iOS,但我不想使用无用的自动填充代码来填充我的代码。因此,当用户在Google的自动完成中输入他们的位置时,调用函数pickwash()并且动画不起作用。当我在带有按钮的IBAction中使用它时,它才对我有用。有什么想法吗?

3 个答案:

答案 0 :(得分:1)

isHidden无法使用alpha动画,请参阅以下代码。 我注意到你把时间设置为10.0和10这么长。

func pickWash() {

    UIView.animate(withDuration: 1.0, animations: {
        self.view.layoutIfNeeded()
        self.waterlessLabel.alpha = 1
        self.exteriorInterior.alpha = 1
        self.exteriorOnly.alpha = 1
        self.info.alpha = 1
        self.blackLine.alpha = 1
        self.extIntPrice.alpha = 1
        self.extPrice.alpha = 1
        self.confirmWash.alpha = 1
        self.when.alpha = 1
        self.timeChoice.alpha = 1

    }, completion: nil)

}

func tester(){
   self.pickWash()
}

答案 1 :(得分:1)

pickWash()正处于主要线程以外的线程上(如OP注释中所确认的),并且由于主线程是唯一允许在UI上工作的线程,因此行为未定义(此处没有任何反应)。您必须使用

将代码执行移动到主线程
func pickWash() {
    // Code here is on a non-main thread
    DispatchQueue.main.async {
        // Code here is executed on the main thread 
        bottomChange = self.mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -350.0)
        bottomChange.isActive = true

        UIView.animate(withDuration: 10.0, animations: {
            self.view.layoutIfNeeded()
            self.waterlessLabel.isHidden = false
            self.exteriorInterior.isHidden = false
            self.exteriorOnly.isHidden = false
            self.info.isHidden = false
            self.blackLine.isHidden = false
            self.extIntPrice.isHidden = false
            self.extPrice.isHidden = false
            self.confirmWash.isHidden = false
            self.when.isHidden = false
            self.timeChoice.isHidden = false

        }, completion: nil)
    }
}

func tester(){
   self.pickWash()
}

答案 2 :(得分:0)

如果要为约束设置动画,您只需更新常量属性即可。这是一些示例代码:

@IBOutlet private weak var mapViewBottomConstraint; // If you are not using InterfaceBuilder, then hold a reference to the bottom constraint when you add it to your view.

func pickWash() -> Void {
    self.mapViewBottomConstraint.constant = -350.0; // or whatever is appropriate here.
    UIView.animate(withDuration: 1.0, animations: { [weak self] in
        self?.view.layoutIfNeeded()
        // do your stuff
    }, completion: nil)
}