ios - UIImageView 动画在 UICollectionViewCell 中不能正常工作

标签 ios swift animation uiimageview uicollectionviewcell

我想在 UICollectionViewCell 中添加一个 UIImageView 动画,所以我想出了这段代码:

    import UIKit

class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var items:[String] = ["one", "two", "three", "four"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
        self.view.addSubview(self.collectionView)
    }

    lazy var collectionView:UICollectionView = {
        var cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: self.flowLayout)
        cv.delegate = self
        cv.dataSource = self
        cv.bounces = true
        cv.alwaysBounceVertical = true
        cv.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
        cv.registerClass(CustomCell.self, forCellWithReuseIdentifier: "cell")
        cv.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0)
        return cv
    }()

    lazy var flowLayout:UICollectionViewFlowLayout = {
        var flow = UICollectionViewFlowLayout()
        flow.sectionInset = UIEdgeInsetsMake(2.0, 2.0, 2.0, 2.0)
        return flow
    }()


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{

        let width:CGFloat = self.view.bounds.size.width*0.98;
        let height:CGFloat = 150.0;
        return CGSizeMake(width, height)
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return self.items.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCell
        cell.layer.cornerRadius = 4
        cell.backgroundColor = UIColor.whiteColor()
        cell.imgView.animationImages = ["1","2","3","4","5", "6","7","8"].map{UIImage(named: $0)!}
        cell.imgView.animationDuration = NSTimeInterval(0.8)
        cell.imgView.startAnimating()
        return cell
    }

    func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
         return true
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        var alert = UIAlertController(title: "Alert!", message: "Cell tapped", preferredStyle: UIAlertControllerStyle.Alert)
        var action = UIAlertAction(title: "ok", style: UIAlertActionStyle.Cancel) { (dd) -> Void in }
        alert.addAction(action)
        self.presentViewController(alert, animated: true, completion: nil)
    }

}

这是我的 MainViewController,以编程方式添加了一个 CollectionView

    import UIKit

class CustomCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.imgView)
    }

    lazy var imgView:UIImageView = {
        var iv = UIImageView()
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        return iv
    }()

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.imgView.frame = CGRectMake(6,15,self.frame.width*0.2, self.frame.height*0.8)
    }
}

这是我的自定义 UICollectionViewCell 和一个 UIImageView

我的问题是当您点击一个单元格时 UIImageView 消失了。为了解决这个问题,我开始寻找另一种 UICoolectionView delegate 方法。然后我尝试使用 shouldHighlightItemAtIndexPath,但是如果我使用此方法返回 false 动画工作正常但 Collection View 停止响应 didSelectItemAtIndexPath

这是一个 github 存储库,其中包含显示问题的代码:https://github.com/gazolla/ImageAnimation (更新了解决方案)

解决方案:

在 matt 的帮助下,我对代码进行了以下更改:

1) 将图像数组添加到 highlightedAnimationImages 属性

let animationArray = ["1","2","3","4","5", "6","7","8"].map{UIImage(named: $0)!}
cell.imgView.highlightedAnimationImages = animationArray

2) 取消选择单元格时重新启动动画

 func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CustomCell{
        cell.imgView.startAnimating()
    }
 }

3) 选择单元格时重新启动动画

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CustomCell
    cell.imgView.startAnimating()

    //...

}

解决方案 2:

经过一些测试,我发现当你tap and hold一个单元格时,UIImageView又消失了,所以我们必须用另外两种方法重新启动动画:

1) didHighlightItemAtIndexPath

func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
        if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CustomCell{
            cell.imgView.startAnimating()
        }

  }

2) didUnhighlightItemAtIndexPath

func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CustomCell{
        cell.imgView.startAnimating()
    }
}

最佳答案

当您选择单元格时, ImageView 查看其 highlightedAnimationImages,而不是其 animationImages。您没有设置 highlightedAnimationImages,所以您什么也看不到。

这是一个截屏视频,显示它可以工作。当背景为灰色(即它的 selectedBackgroundView)但动画继续时,单元格被选中:

enter image description here

关于ios - UIImageView 动画在 UICollectionViewCell 中不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32356352/

相关文章:

ios - 缩放图像 : how can the accelerate be the slowest method?

ios - Azure 通知中心注册设备列表

ios - 如何整合照片库

objective-c - 检测绘制图像的百分比是 "erased"

ios - 长按快速将图钉注释添加到 map View

android - 如何在 Android 中同时对 View 进行动画、缩放和变换?

ios - Swift 中的 Snapchat API

android - 来自 Android 中自定义 URL 的 React-native jsbundle

java - NineOldAndroids 动画不适用于 API > 10

animation - 如何在javaFX中使用KeyPress事件获得流畅的动画?