Swift Animate duration not working in CGAffineTran

2019-08-27 03:28发布

问题:

When I am translating one view with an animation of 1 second it is not working, but when I am executing the transform.identity it works fine.

Here is my code:

func hideCarousel() {
    UIView.animate(withDuration: 1, animations: {
        self.carouselER.transform  = CGAffineTransform(translationX: 0, y: 200)
    })
}

func showCarousel() {
    UIView.animate(withDuration: 1, animations: {
        self.carouselER.transform = .identity
    })
}

回答1:

To solve this issue I forced the animation to run in the main thread. Every time that you have problems with the performance of your UI elements like your animations or updating your label texts, try forcing to run the UI change in the main thread.

    DispatchQueue.main.async {
        UIView.animate(withDuration: 1, animations: {
            self.carouselER.transform  = CGAffineTransform(translationX: 0, y: 200)
        })
    }

I have also faced that problem with one timer that updated a label, but in this issue I thought it was some kind of problem of the CGAffineTransform.