Whirlwind Whirlwind - 1 year ago 116
Swift Question

Animation stops when cell disappear and not resume when appear again

I am having simple animation (actually two of them, but will show only one for simplicity of an example) which continuously fades in, and fades out a label. So, when I scroll the tableview in such a way that some animated cells disappear, and then scroll back, those cells aren't animated anymore.

Here is the animation:

delay: 0.0,
options: [.Repeat, .Autoreverse, .AllowUserInteraction],
animations: {
self.stateLabel.alpha = 0.5
}, completion: nil)

I start this animation in method called setup() which is an instance method of my custom cell class (which inherits from
). As I said, there is another animation which behaves the same, and it shakes the imageview, so I am positive that this is not related to fading kind of an animation.

I invoke
method in
method after dequeuing.

What I have tried is to run this
method in
method, but the result is the same (I am not dequeueing cell in this method of course).

Also I tried with break points, and the animation code is executed. Just, animation doesn't start when cell re-appears. So the question is, how to restart / continue with animation when cell appears again?

Answer Source

Well the problem is not resetting the alpha value of the UI element after displaying it. Because UITableView would restore but not recreate the cell if dequeueReusableCellWithIdentifier function is being used. Thus resetting the alpha value on cellForRowAtIndexPath and animating the view in willDisplayCell would solve the problem you were facing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download