Search code examples
swiftxcodeuikit

UIView.animate() issue


I have a code, where UI elements are changed after user interaction. I use UIView.animate() to refresh UI. The code is pretty simple:

UIView.animate(withDuration: 1.0) {
    self.fadeIn()
} completion: { _ in
    UIView.animate(withDuration: 1.0) {
        self.fadeOut()
    }
}

Very important point is that total animation duration from beginning to the end should be 2 seconds. But sometimes, there are no UI changes in fadeIn(), and UIView.animate() just get immediately to completion block. But in that option I want to have a delay for duration time (1 sec), and only after that get to completion block to fadeOut. How I can force animate with duration even if it's nothing to animate in animation block? Thanks in advance!


Solution

  • Maybe don't call fadeOut() in the completion block, and just make sure that it's called with one second delay after the fadeIn() is called?

    UIView.animate(withDuration: 1.0) {
        self.fadeIn()
    } 
    
    UIView.animate(withDuration: 1.0, delay: 1.0) {
        self.fadeOut()
    }