0

我需要在 UIImageView 中的图像之间制作动画。因此,经过研究,我得出了这门课:

    import UIKit

    class GzImageView: UIImageView {

    var imageCollection:[UIImage]? //images to show
    var count:Int = 0 // images counter

    func animate(){
        //when Counter get last image set it to the first one
        if self.count >= self.imageCollection?.count {
            self.count = 0
        }
        //get the image to show
        let img = self.imageCollection![self.count]
        //animate the transition
        UIView.transitionWithView(self,
            duration: 1.0,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: { self.image = img }) //set the new image
            { (Bool) -> Void in
            self.animate() //Direct recursion
            self.count++
        }
    }                     
 }

我这样使用它:

let imgView = GzImageView(frame: CGRectMake(30, 100, 200, 200))
imgView.imageCollection = ["cake.png","bag.png","hat.png"].map { name -> UIImage in UIImage(named:name)!}
imgView.animate()

当我用作 UIView 子视图时,它工作正常。但我需要将其用作 UICollectionViewCell 子视图。作为 UICollectionViewCell 子视图,它不能正常工作。当我更改为其他 UIView 并返回 UICollectionView 时,图像开始快速闪烁。

我无法找出问题所在,所以我正在寻找另一种方法来获得此功能。

你们还有其他方法可以做到这一点或知道如何解决吗?

4

2 回答 2

-1

对于 Swift 3.0.1 尝试https://stackoverflow.com/a/42710002/4935811

UIView.transition(with: self.imageView,
          duration:0.5,
          options: .transitionCrossDissolve,
          animations: { self.imageView.image = newImage },
          completion: nil)

参考:https ://gist.github.com/licvido/bc22343cacfa3a8ccf88

于 2017-03-10T04:07:17.760 回答
-2

您可以通过以下方式轻松完成:

var animationDuration = 3;

            imageView.animationImages = imageSet;
            imageView.animationDuration = NSTimeInterval(animationDuration);
            imageView.startAnimating();

这里是 UIImage 对象数组中的 imageSet。

于 2015-08-28T13:23:37.837 回答