如何使用按钮点击开始动画?

时间:2017-01-27 13:46:57

标签: ios swift animation uicollectionviewcell collectionview

在我从UICollectionViewCell点击“播放”按钮后,我似乎无法弄清楚如何开始制作动画。以下是我的一些代码,请帮忙吗?

我有其他与我的collectionView设置相关的代码,但不确定是否需要查看。

看来,我只能在viewAppears之后运行动画,但是如何在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是我的一些UIViewController代码:

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
   }

1 个答案:

答案 0 :(得分:1)

似乎是你想要的情况:当在单元格中点击按钮时,视图控制器应该执行一些动画?这个问题来自于需要在单元和视图控制器之间进行更好的协调。细胞只是视图,没有知识可以在自己之外做任何事情。

当视图控制器在cellForItemAtIndexPath中格式化单元格时,您需要为其指定“执行动画委托”performAnimationDelegate。这是一个返回视图控制器的参考。

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 
    }

}

请注意单元格委托上的弱变量,因为您不希望单元格保持视图控制器处于活动状态。

这不是实现这一目标的唯一方法,但它已经建立并且简单。请记住,委托(视图控制器)没有任何关于调用它的信息,因此您必须添加参数或检查是否要知道例如正在挖掘哪个单元。希望这可以帮助。