UIImageView在动画过程中无法更改背景颜色和设置图像。

5
作为标题所说,这很奇怪,你同意吗?
如果有人发现了你的代码
imageView.backgroundColor = [UIColor greenColor]; 

或者

imageView.image = [UIImage imageName:@"angryBird"];" 

如果您的imageView正在进行动画,或者动画是否已被删除,请注意它不起作用。

----------以下是答案---------------

在设置图像和更改正在进行动画的UIImageView的背景之前,请停止动画。

UIImageView* imageView = [UIImageView alloc] init];
//......do sth. like setFrame ...
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array 
imageView.animationDuration = 1.0;
[imageView startAnimating];
//......do sth.
[imageView stopAnimating];        // **** do not forget this!!!!! ****
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageName:@"angryBird"];

当你执行 performSelector: withObject: afterDelay:1.0s 方法时,在选择器方法中,你还需要停止动画,然后设置图片或更改背景颜色。

4个回答

5

尝试在动画完成后更改背景颜色并更改图片。这将对您非常简单。


1

试试这个:

[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];

0

尝试使用这段代码,并以以下方式执行performSelector:withObject:afterDelay::

    [self performSelector:@selector(animationDidFinish:) withObject:nil
         afterDelay:instructions.animationDuration];

或者使用 dispatch_after:

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
         [self animationDidFinish];
    });

希望能对你有所帮助


在将图像名称指定为angryBird之前,请仔细检查您的拼写。 - chandan
谢谢。这不是框架的问题。我在动画停止之前更改了imageView的背景颜色,正确的方法是在动画停止后更改背景颜色。或者,另一种方式是停止动画,然后更改其背景颜色。 - isaacselement
你真正想要什么?你想在动画停止之后还是之前改变背景颜色? - chandan
是的 :),它的动画必须停止,并且更改背景颜色会起作用。 - isaacselement
他的想法是正确的。我真正想指出的是:如果有人想要更改UIImageView的背景颜色或图像,他应该停止其动画。在“imageView.backgroundColor = [UIColor greenColor]”之前放置代码“[imageView stopAnimating]”。如果不这样做,设置背景颜色将失败。非常感谢@chandan,:) - isaacselement

0

尝试这个,使用块进行动画

[UIView animateWithDuration:1.0 animations:^{
    }
    completion:^(BOOL finished)
    {
        [imageView setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME"]];
        [imageView setBackgroundColor:[UIColor greenColor]];


    }];

注意:它是imageNamed:而不是imageName:,我不确定这个会如何编译。

抱歉,忘记了“imageName:”和“愤怒的小鸟”,我并没有在XCode中编写它。我只是在这个文本区域里写的。 ;) - isaacselement

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接