marcosgriselli / ViewAnimator

ViewAnimator brings your UI to life with just one line
MIT License
7.3k stars 488 forks source link

Animate on viewDidLoad() #30

Closed hallamager closed 6 years ago

hallamager commented 6 years ago

How can I use an animate a UITableView's cells on viewDidLoad()?

marcosgriselli commented 6 years ago

Hi @hallamager you want to run an animation when the ViewController is displayed right? The best way to do this is to force the tableView to reload (or load initially in this case as at that point the cells have not been loaded yet) and then run your animation. I just tested this code on the example app and it works:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData()
    let animations = [AnimationType.from(direction: .bottom, offset: 30.0)]
    tableView.animateViews(animations: animations)
}
hallamager commented 6 years ago

That's perfect thank you!

hallamager commented 6 years ago

Also, i’m trying to implement an animation on a collectionView with a delay. However the collectionView flashes quickly and them does the animation.

marcosgriselli commented 6 years ago

@hallamager please check #23, UICollectionView cell reloading works different from UITableView so you have to implement a workaround for it.

hallamager commented 6 years ago

Bit new to swift, this is confusing me a little. I'm trying to do this for viewDidLoad again. This is what i've got;

self.collectionView.performBatchUpdates({ self.collectionView.animateViews(animations: self.animations, delay: 0.3) }, completion: nil)

marcosgriselli commented 6 years ago

Right that looks ok but make sure to call collectionView.reloadData() before that to make sure the cells are loaded.

hallamager commented 6 years ago

Same problem still occurs.

I've got;

self.collectionView.reloadData() self.collectionView.performBatchUpdates({ self.collectionView.animateViews(animations: self.animations, delay: 0.3) }, completion: nil)