Reputation: 19144
I have this animation block, but the cell
background color is clear
immediately:
cell.mylabel.backgroundColor = .orange
UIView.animate(withDuration: 3.0, delay:3, animations: {
cell.mylabel.backgroundColor = .clear
})
The point is I am running this in cellForRowAt
I tried moving animation to my custom cell, but no success again:
override func didMoveToWindow() {
UIView.animate(withDuration: 3.0, delay:3, animations: {
self.mylabel.backgroundColor = .clear
})
}
Upvotes: 2
Views: 106
Reputation: 385540
The point is I am running this in
cellForRowAt
That is too early, because the cell may not be in the on-screen view hierarchy yet. You can only animate a view that's in the on-screen view hierarchy. You need to find a way to add the animation after the cell has been added as a subview of the table view.
I would create a custom UITableViewCell
subclass. In the subclass, I would override didMoveToWindow
to add the animation to self
.
Alternatively, it might work to override viewDidLayoutSubviews
in the table view controller (if it has one), and add the animation in that method.
Upvotes: 2
Reputation: 658
Try like this,
cell.mylabel.layer.backgroundColor = UIColor.redColor().CGColor
UIView.animate(withDuration: 3.0, delay:3, animations: {
cell.mylabel.layer.backgroundColor = UIColor.clearColor().CGColor
})
Upvotes: 1