Search code examples
iosswiftanimationuicollectionviewcellcollectionview

How to use button click to start animation?


I can't seem to figure out how to start an animation after my "Play" button has been clicked from my UICollectionViewCell. Here is some of my code below, any help please?

I have other code that pertains to my collectionView setup but not sure if you need to see it or not.

It seems, I'm only able to run the animation once the viewAppears but how do you initiate an animation well after the viewAppears?

Here is my UICollectionViewCell code:

import UIKit

class CreateCollectionViewCell: UICollectionViewCell {

    var animateDelegate: AnimateScenesDelegate!    

@IBOutlet weak var scenes: UIImageView!

@IBAction func scenePlay(sender: UIButton) {

    animateDelegate.animateScenes()

    let playButtonFromCreateCollection = scenes.image!

    print("It was this button \(playButtonFromCreateCollection)")

      }

  }

HERE is some of my UIViewController Code:

class CreateViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, AnimateScenesDelegate {

    @IBOutlet weak var StoryViewFinal: UIImageView!

    var scene01_68: [UIImage] = []

    func animateScenes () {

        print("Play button was pressed")
        StoryViewFinal.animationImages = scene01_68
        StoryViewFinal.animationDuration = 15.0
        StoryViewFinal.animationRepeatCount = 1
        StoryViewFinal.startAnimating()

       }

    func loadScenes () {
       for i in 1...158 {
          scene01_68.append(UIImage(named: "Scene01_\(i)")!)
          print(scene01_68.count)
         }
      }


 override func viewDidAppear(animated: Bool) {

        animateScenes()

 super.viewDidLoad()


    loadScenes ()


func collectionView(collectionView: UICollectionView,     cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
\\ OTHER CODE...
     cell.animateDelegate = self
     return cellA
   }

Solution

  • It seems to be the case that you want: When a button is tapped in the cell, the view controller should perform some animation? This issue comes from needing better coordination between the cell and the view controller. Cells are just views and don't have the knowledge to do anything outside themselves.

    When the view controller formats the cell in cellForItemAtIndexPath, you need to give it a "perform animation delegate" performAnimationDelegate. This is a reference back to the view controller.

    protocol AnimateScenesDelegate {
        func animateScenes()
    }
    
    class CreateCollectionViewCell: UICollectionViewCell {
        weak var animateDelegate : AnimateScenesDelegate
    
        @IBAction func scenePlay(sender: UIButton) { 
             animateDelegate?.animateScenes()
        }
    }
    
    class CreateViewController: UIViewController, ... AnimateScenesDelegate { 
    
        func animateScenes() {
            //Animate here ... 
        }
    
        func collectionView(_ collectionView: UICollectionView, 
      cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            //...
            cell.animateDelegate = self 
        }
    
    }
    

    Note the weak var on the cell delegate, because you don't want the cell to keep the view controller alive.

    This is not the only way to do this but it's established and simple. Remember that the delegate (view controller) doesn't have any information about what is calling it, so you would have to add a parameter or check if you wanted to know for example which cell is being tapped. Hope this helps.