2015-08-28 167 views
0

我需要在UIImageView中的图像之间进行动画。所以,研究后,我想出了这个类: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图像开始快速闪烁。

我找不出这个问题,所以我正在寻找另一种获得此功能的方法。

你们有另一种方式来做到这一点,或知道如何解决这个问题吗?在UIImage的对象数组

var animationDuration = 3; 

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

这里imageSet:

回答

-1

您可以轻松地这样做。

+0

它工作正常,但是这个代码不具备的动画之间的淡入淡出效果。你知道在这个实现中添加这个效果吗? – Sebastian