我需要在 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 时,图像开始快速闪烁。
我无法找出问题所在,所以我正在寻找另一种方法来获得此功能。
你们还有其他方法可以做到这一点或知道如何解决吗?