Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用按钮单击开始动画?_Ios_Swift_Animation_Uicollectionviewcell_Collectionview - Fatal编程技术网

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

Ios 如何使用按钮单击开始动画?,ios,swift,animation,uicollectionviewcell,collectionview,Ios,Swift,Animation,Uicollectionviewcell,Collectionview,在从UICollectionViewCell单击播放按钮后,我似乎不知道如何启动动画。下面是我的一些代码,有什么帮助吗 我有其他与collectionView设置相关的代码,但不确定您是否需要查看它 看起来,我只能在视图出现后运行动画,但如何在视图出现后启动动画 Here is my UICollectionViewCell code: import UIKit class CreateCollectionViewCell: UICollectionViewCell { var a

在从UICollectionViewCell单击播放按钮后,我似乎不知道如何启动动画。下面是我的一些代码,有什么帮助吗

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

看起来,我只能在视图出现后运行动画,但如何在视图出现后启动动画

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代码:


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

当视图控制器格式化cellForItemAtIndexPath中的单元格时,需要为其指定一个执行动画委托PerformManimationLegate。这是对视图控制器的引用

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 
    }

}
请注意单元格委托上的弱var,因为您不希望该单元格使视图控制器保持活动状态


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

您希望启动哪种类型的动画以及何时启动?您的问题还不清楚。您好,谢谢@Mady,我想启动func animateScenes,它包括和图像数组,我想为animateThanks@MikeSand制作动画。我使用您的代码使它工作,但我不得不强制展开animateDelegate属性,因为类错误声明它没有任何初始值设定项。我更新了上面的代码。有没有办法确定通过scenePlay按钮点击了哪个单元格?
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 
    }

}