Wasif Khalil
Wasif Khalil

Reputation: 2247

UIView animate finished called before animation finished

I'm trying to create an animation where the view first animate down and then gets removed from super view, my code looks like this:

UIView.animate(withDuration: 5, delay: 5, options: .allowAnimatedContent, animations: {
        NSLog("Animation started")
        self.scrollView.setContentOffset(CGPoint(x:0,y:-500), animated: true)

    }, completion: {(finished: Bool) in
        if finished{
            NSLog("Animation stopped")
            self.view.removeFromSuperview()
        }
    })

As the duration is 5 so the finished should gets called after 5 seconds or I am wrong?

In this case the finished gets called before the animation is finished and removed the view and animation obviously didn't show at all because the view was removed.

Here is the NSLog

2017-01-02 17:39:37.649 [1581:26706] Animation started
2017-01-02 17:39:37.652 [1581:26706] Animation stopped

finished was called in less than a seconds

Upvotes: 1

Views: 1624

Answers (1)

Venk
Venk

Reputation: 5955

Try setting setContentOffset: function animated to false

UIView.animate(withDuration: 5, delay: 5, options: .allowAnimatedContent, animations: {
    NSLog("Animation started")
    self.scrollView.setContentOffset(CGPoint(x:0,y:-500), animated: false)
}, completion: {(finished: Bool) in
    if finished{
        NSLog("Animation stopped")
        self.view.removeFromSuperview()
    }
})

Upvotes: 4

Related Questions