ios - 如何使用按钮点击启动动画?

标签 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)")

      }

  }

这是我的一些 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
   }

最佳答案

这似乎是您想要的情况:当点击单元格中的按钮时, View Controller 应该执行一些动画?这个问题来自需要在单元格和 View Controller 之间更好地协调。单元格只是 View ,不具备在自身之外做任何事情的知识。

当 View Controller 格式化 cellForItemAtIndexPath 中的单元格时,您需要给它一个“执行动画委托(delegate)”performAnimationDelegate。这是对 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 
    }

}

请注意单元格委托(delegate)上的 weak var,因为您不希望单元格保持 View Controller 处于事件状态。

这不是唯一的方法,但它是既定又简单的方法。请记住,委托(delegate)( View Controller )没有任何关于调用它的信息,因此您必须添加一个参数或检查您是否想知道例如正在点击哪个单元格。希望这可以帮助。

关于ios - 如何使用按钮点击启动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41895438/

相关文章:

ios - CAGradientLayer 不填充宽度

ios - 旧学校类(class)开始

ios - 设置 Spritekit 重力源

ios - 设置 CALayer masksToBounds 导致图层消失

swift - 核心蓝牙状态恢复只触发一次

android - 当在 LottieAnimationView 中插入我的 json 文件时,发生了 "error null object reference"(Android)

swift - 如何快速使用代码从一个 View Controller 转到另一个 View Controller

iphone - 我想添加一个删除帖子按钮

ios:条件绑定(bind)的初始化程序必须具有可选类型,而不是 'LOTAnimationView'

javascript - JQuery animate() 函数延迟运行缓慢