0
votes

I'm using UIView.animated(withDuration:animations:completion:) function and there are sometimes that there is no animations affected in the animations block

For example:

Let's assume that I have a view, and it's frame.origin.y is already equals to 0.

Now the animation that I wan't to make is that:

UIView.animate(
    withDuration: 1,
    animations: {
        self.view.frame.origin.y = 0
    }
    completion: { completed in
        guard completed else { return }
        // do something
    }
)

The completion block called after 1 second instead of instantly.

How can I make that the completion block will called instantly if there are no animations affected in the animations block without any duration.

2
Technically I believe there is still an animation if you assign to an animatable property even if the animation is unnoticeable to the user, so just because origin.y was already 0 doesn't mean there wouldn't be an animation. You need to check for this case yourself and just not animate.Arkku

2 Answers

2
votes

This is a something that you should handle yourself , the animations won't know that , you can make a compare like

 if self.view.frame.origin.y != someValue {
    // do animation
 }
 else {
    // run some other code
 }
1
votes

Replace

withDuration: 1,

With

withDuration: 0.01,

(Or even less)