Reputation: 775
I have a UISlider
in my table view's reusable cell.
As soon as the value changes, a custom method valueChanged()
gets called, which triggers the table view's reloadData()
method.
The reloadData()
calls cellForRowAtIndexPath
and as soon as the cell in which the slider was changed gets loaded, it animates two constraints which should make it look as if a button is sliding in from the right of the cell.
Basically, the effect I'm trying to create is that as soon as the user starts sliding the UISlider
the animation takes place and the button appears.
Here's the catch though: the UISlider
's isContinious
property is set to true
to make sure the animation happens right away, and doesn't get delayed until the user takes his finger of the slider. Setting isContinious
to true
, however, makes it that reloadData()
(which was inside my custom valueChanged()
) gets called A LOT OF TIMES with every little slide the user commits.
After doing some research, and trying out a lot of debugging tactics, I now know that as soon as reloadData()
gets called, any running animations (which are still animating at the time reloadData()
gets called) get interrupted and just move to the state it's supposed to be in after the animation is done.
So to recap the scenario:
1) slider moves,
2) valueChanged()
triggers reloadData()
,
3) animation starts to update layout,
4) reloadData()
immediately gets called again ruining the animation which started in step 3.
Everything is happening so fast when isContinious
is set to true
, so the animation doesn't get the time it needs to complete before reloadData()
gets called again. This ruins the animation and makes it look as if the button is just appearing on the screen without animation, even though I know UIView.animate(withDuration:)
DOES get called and does its job normally. I was wondering if there's a way to refresh a table without calling reloadData()
, so basically an alternative to this method (I've already tried reloadCellAtIndexPath
and this has the exact same effect). If not, is there a way to make absolutely sure that an animation finishes without being interrupted by any other layout updating method?
Upvotes: 2
Views: 1047
Reputation: 16327
If the problem is the refresh kills the animation you can defer the reloadRowsAtIndexPaths call until the animation has finished executing. Normally I would say call it in the completion handler of your animation block but it seems like your value changed may be firing off more than one animation. Instead when you start the animation also start a timer that will run a block to execute the reload. If you start a new animation, invalidate the old timer and make a new one. the reload will only execute when the final animation is done.
var timer = Timer()
func valueChanged() {
timer.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: animationDuration, repeats: false) { _ in
//Call reload here
}
//Animate here
}
Upvotes: 1