2012-11-23 135 views
0

我有7个图像,我试图淡入/出1 ImageView。我已经将所有图像存储在一个数组中,然后有一个循环来显示每个图像并淡入下一个,但是,当我运行程序时,只显示最后2张图像。任何想法,为什么这是?以及如何解决它?代码如下。与UIImageView上的动画故障

imageArray = [NSArray arrayWithObjects: 
       [UIImage imageNamed:@"image 0.jpg"], 
       [UIImage imageNamed:@"image 1.jpg"], 
       [UIImage imageNamed:@"image 2.jpg"], 
       [UIImage imageNamed:@"image 3.jpg"], 
       [UIImage imageNamed:@"image 4.jpg"], 
       [UIImage imageNamed:@"image 5.jpg"], 
       [UIImage imageNamed:@"image 6.jpg"], 
       nil]; 
      self.imageview.backgroundColor = [UIColor blackColor]; 
       self.imageview.clipsToBounds = YES; 

int count = [imageArray count]; 
for (int i = 0; i <count-1 ; i++) 
{ 
    UIImage *currentImage = [imageArray objectAtIndex: i]; 
    UIImage *nextImage = [imageArray objectAtIndex: i +1]; 
    self.imageview.image = [imageArray objectAtIndex: i]; 
    [self.view addSubview:self.imageview]; 
    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"]; 
    crossFade.duration = 5.0; 
    crossFade.fromValue = (__bridge id)(currentImage.CGImage); 
    crossFade.toValue = (__bridge id)(nextImage.CGImage); 
    [self.imageview.layer addAnimation:crossFade forKey:@"animateContents"]; 
    self.imageview.image = nextImage; 

}; 

回答

1

需要注意以下几点:

1)你是在具有每个转换之间没有延迟一个循环同步做的一切,有一个在动画化是没有意义的,如果下一次循环将被替换下一个等等,直到最后2.另外,你每次都将你的imageView作为子视图添加,也是不必要的。

2)您正在更改imageView的.image属性,然后更改图层的内容。不妨使用UIView并具有相同的效果。

我的建议是制作一种方法从一个图像切换到另一个图像,并且每隔x秒钟调用一次函数,直到您全部通过它们。

编辑:对于未来:

self.imageview.backgroundColor = [UIColor blackColor]; 
self.imageview.clipsToBounds = YES; 

是完全没有必要的:)

.backgroundColorUIImage您显示绘制在。

.clipsToBoundsUIImageView默认行为(你的形象它缩小/扩大它的规模,但从来没有画外)