我正在尝试为一系列图像添加动画。
图像之间的变化不一定要有动画,但我使用动画来控制时间:
-(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];
}
}];
}
图像正在变化,但无论我在持续时间中使用什么,它都会忽略时间并尽可能快地进行。
如果我更改 Alpha,也会发生同样的情况:
-(void)nextImage
{
[UIView animateWithDuration:0.5 animations:^{
self.imageView.alpha = 1 - index++/100
}completion:^(BOOL completed){
if (index < 50)
{
[self nextImage];
}
}];
}
最佳答案
只有UIView
的一些属性是animatable:
@property frame
@property bounds
@property center
@property transform
@property alpha
@property backgroundColor
@property contentStretch
UIImageView
的 image
属性不可设置动画。
如果您要更新 UIImageView
内的图像,请使用不同的技术,例如 block :
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),
dispatch_get_current_queue(), ^{
// update your image
});
(替代方案:performSelectorAfterDelay:
或 NSTimer
。不过,我建议使用 block 。)
我认为由于您的除法中的 int 截断,您的 alpha 动画无法正常工作。试试这个:
self.imageView.alpha = 1.0f - (float)index++/100.0f;
原始除法的问题在于表达式a/b
(两者都是整数)被执行为整数除法。因此,如果 a
<b
,结果将为 0
——换句话说,所有值的 alpha 设置完全透明。
关于ios - ImageView 动画时的持续时间被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566138/