2013-10-24 71 views
2

我正在尝试动画化一系列图像。忽略动画ImageView的持续时间

图像之间的变化并不一定有一个动画,但我使用动画来控制时间:

-(void)nextImage 
{ 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]]; 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
} 

图像被改变,但无论我在持续使用,它忽略时间并尽可能快地进行。

如果我改变α,同样的情况:

-(void)nextImage 
{ 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.alpha = 1 - index++/100 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
} 

回答

3

UIView某些属性是animatable:

@property frame 
@property bounds 
@property center 
@property transform 
@property alpha 
@property backgroundColor 
@property contentStretch 

一个UIImageViewimage属性不是动画。

如果你有更新UIImageView内的图像,而不是使用不同的技术,如块:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),  
    dispatch_get_current_queue(), ^{ 
     // update your image 
    }); 

(备选:performSelectorAfterDelay:NSTimer我建议使用块虽然)。

我认为您的alpha动画由于您的分区中的int截断而不起作用。试试这个:

self.imageView.alpha = 1.0f - (float)index++/100.0f; 

与你原来的分工问题是表达a/b,其中两个都是整数,作为整数除法进行。因此,如果a < b,结果将为0 - 换句话说,对于所有值完全透明的alpha设置。

+0

好吧,但为什么动画的阿尔法不工作? – ReloadC

+0

看到我的答案... –

1

是不可能的[UIView animateDuration:animations:completion]; 尝试使用的NSTimer并调用改变图像的每一个步骤的功能做到这一点。只有

-1

为什么不使用ImageView的images属性并设置要设置动画的图像数组?

+0

这将简单地改变显示的图像,不会有动画。 –

+0

请参阅原始问题:“图片之间的变化不一定要有动画” – slecorne

2

阿尔法不工作,因为你写

self.imageView.alpha = 1 - index++/100; 

这里的一切是int,因此您的结果只能是整数值,即1或0。使用此相反:

self.imageView.alpha = 1.0f - index++/100.0f; 

编译器将能够隐式转换index为float,但你可以明确的写:

self.imageView.alpha = 1.0f - (CGFloat)(index++)/100.0f; 
0

可以使用的UIImageView动画属性太,例如:

// arrayWithImages 
arrayPictures = [[NSMutableArray alloc] initWithCapacity:50]; 

// The name of your images should 
for (int i=0; i<=49; i++) { 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d.jpg",i]]; 
    [arrayPictures addObject:image]; 
} 

imageView.animationDuration = 0.5; 
imageView.animationImages = arrayPictures; 

[imageView startAnimating]; 

根据图像的数量,你可以有一些内存的问题,就必须使用动画较低级解决方案的图像。

0

如果他们只是动画持续时间问题,那么可能是动画未启用,因为我在我的iOS应用程序中遇到了这个问题。这里是简单的解决方案:

只有你需要添加[UIView setAnimationsEnabled:YES];在开始动画块之前。所以你的完整代码将是这样的:

-(void)nextImage 
{ 
    [UIView setAnimationsEnabled:YES] 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]]; 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
}