简体   繁体   中英

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

In actuality, my tester method in my code is using Google's Place autocomplete iOS, but I did not want to flood my code with useless autocomplete code. Thus, when the user is done entering their location in Google's autocomplete, the function pickwash() is called and the animate does not work. It only worked for me when I had it in an IBAction with a button. Any idea?

isHidden can't be animated use alpha , see the below code. and I noticed that you are setting the time to 10.0 and 10 is so long.

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() is being on a thread other than the main one (as confirmed in OP comments) and since the main thread is the only one allowed to work on the UI the behaviour is undefined (here nothing happens). You have to move your code execution to the main thread using

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

If you want to animate a constraint, all you have to do is update the constant property. Here's some sample code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM