Animation stops when cell disappear and not resume when appear again - swift

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:
UIView.animateKeyframesWithDuration(
0.5,
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 UITableViewCell). 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 setup() method in cellForRowAtIndexPath method after dequeuing.
What I have tried is to run this setup() method in willDisplayCell 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?

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.

Related

Animations set in collectionView keep reloading

I have a collectionView and each cell has a graph that animates once the collectionView loads. The animation is based on CAShapeLayer animation and the value is being passed from the collectionView.
The animation works somewhat fine for most cells. The two things that I am running into are:
1) When I scroll down and new cells appear, the animation for those newly appeared cells begin, but the progressLabel only appears when the animation is finished (which is odd).
2) When I scroll back to top, cells that previously already animated will partially animate again.
Is there a way to have all cells animate once the view controller loads and prevent any additional animations?
This is a follow-up to my previous question, which has the code I am using for this: Setting toValue for CAShapeLayer animation from UICollectionView cellForItemAt indexPath

UITableViewCell drawRect() Override Makes Device Really Slow (CPU Usage Spike)

That's pretty much my issue. I want to create rounded plain cells in a table view. I override drawRect(frame: CGRect) in a TableViewCell class, but when the cell is shown, the device (simulator too) freezes for about 1 second and the debugger shows a spike in CPU usage to 75%.
Not that it might be of any use, but here's my code:
override func drawRect(rect: CGRect) {
frame.origin.x = 10
frame.size.width -= 20
}
So is there any easier/MUCH more efficient/"legal"(somebody said overriding drawRect is considered "hacky") way of simply making every cell narrower than the screen?
You're not really allowed to use drawRect to modify a view's frame. drawRect just gives you an opportunity to draw within the given frame.
The actual frame of a UITableViewCell is managed by the UITableView through it's width and the heightForRowAtIndexPath: method. Also, the table view is very efficient at caching and reusing cells. Unless you are displaying 1000's of cell on the screen, you won't need to worry about the efficiency of the drawing.
I think you should be adding a subview to the cell's contentView, and setting it's frame as inset to the cell. This can be done using autolayout in a custom cell xib, or manaully in cellForRowAtIndexPath:.

Run method on first set of UICollectionViewCells, but no on subsequent cells

I have a UICollectionView containing cells that are showing a preview of the camera. I've added a black UIView as a "veil" on top of the cell through storyboard.
When the camera previews begin, I animate the veil UIViews of all cells from an veil.alpha = 1 to veil.alpha = 0. This smooths out the effect of the camera preview starting.
However, this means that when a cell is initialized, but not added to the view, I have to wait until it's added to the view before I can remove the veil UIView. If I attempt beforehand, the veil UIView is nil.
The result is that as I'm scrolling, I catch a glimpse of the veil being animated away for any cells that have not been initialized beforehand.
A simple solution would be to check something like:
"If this cell is being scrolled into visibility, and the camera is already running, before the cell is visible, just set the
veil.alpha = 0."
However, I'm not sure where in the cell/controller lifecycle to put this.
I've tried setting this in prepareForReuse(), layoutSubviews() in the cell, and in cellForItemAtIndexPath in containing controller. All three of those methods cause the veils of the initial visible cells to immediately jump to veil.alpha = 0 without the animation.
Thank you.

Animate UIView setFrame to force drawRect on each animatable frame

I've got a UIView with a UILabel on top. I have the UILabel's content mode set to 'UIContentModeLeft'. As expected, when I animate the frame of the UIView to be smaller than the original size, the label 'jumps' to the final frame without animating nicely.
As far as I can see, UIContentModeRedraw does not force 'drawRect' to be called on every 'animated frame'. I've tried using a custom CALayer as well but can't seem to cause the frame to resize smoothly.
Is there any way to do this? The animation as it stands is extremely glitchy. UIContentModes are not useful and I can't use a frame for contentStretch as well as none of the edges of the label can be stretched. What I really need is a 'refresh' of the label every time the parent view resizes.
There's no way of doing this unfortunately, so I've learnt. The only other way is to actually run a timer that updates the frame every time it's invoked. This results in a very jerky animation given a resize of a complex view is time consuming. I ended up achieving the same thing with some pre-rendered onscreen elements and a whole lotta 'magical effects' behind the scene.

Don't animate a property in transitionWithView

I'm currently flipping a UIImageView using UIView transitionWithView: duration: options: animations: completion: and switching the image inside the animation block. One of the two images it's switching between is landscape and the other is portrait so its also rotating and resizing the imageview so they both fill the screen.
What I can't figure out is how to animate the flip, and rotate it at the appropriate time but not animate the rotation. Is there a way to do this?
You might also try putting the change you don't want to be animated inside a CATransaction begin/commit, with setDisableActions:TRUE. (inside your animation block.) I don't know if it would work or not, but it would be worth a try.