簡體   English   中英

UIView Animate僅在位於IBAction中時才進行動畫處理。 如何獲得適合自己情況的動畫?

[英]UIView Animate only animates if it's in a IBAction. How do I get it to animate in my situation?

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的Place自動填充iOS,但我不想將無用的自動填充代碼充斥於我的代碼中。 因此,當用戶在Google的自動完成功能中輸入完自己的位置后,將調用pickwash()函數,並且動畫不起作用。 只有當我將它放在帶有按鈕的IBAction中時,它才對我有用。 任何想法?

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()
}

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()
}

如果要設置約束的動畫,則只需更新constant屬性。 這是一些示例代碼:

@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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM