UIView动画问题,当尝试没有动画图像时

时间:2012-06-14 08:02:24

标签: iphone ios uiimageview core-animation uiviewanimation

我必须显示图像的幻灯片。但有一刻我只知道当前和以前的图像(因为内存管理)。我需要用动画显示下一个图像(用户可以显示动画类型)。但我看不到动画,只是出现没有动画的新图像。 这是我的代码:

    UIImageView *prevImageView = [self getImageViewWithIndex:currentIndex];
    UIImageView *nowImageView = [self getImageViewWithIndex:newIndex];

    currentIndex = newIndex;
    [UIView animateWithDuration:4 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        [slideShowView addSubview:nowImageView];
    } completion:^(BOOL finished) {
        [prevImageView removeFromSuperview];
    }];

我尝试了不同的动画选项。试图将新图像设置为现有图像视图

nowImageView.image = newImage;

但这没有帮助。

3 个答案:

答案 0 :(得分:5)

animateWithDuration:...内添加或删除视图或设置UIImageView的图像是不可动画的。

使用transitionWithView:...

您可能尝试做的是使用UIView转换(因为您指定了转换选项)。然后你应该使用

transitionWithView:duration:options:animations:completion:

相反,像这样:

[UIView transitionWithView:slideShowView
                  duration:4.0
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^
                { 
                    [prevImageView removeFromSuperview]; 
                    [slideShowView addSubview:nowImageView]; 
                }
                completion:NULL]; // You don't need the completion if you remove the previous image in the animation block.

使用animateWithDuraion:...

如果您真的想使用animateWithDuraion:...(也许还有其他动画同时发生),那么您必须为其他属性设置动画,例如您要添加的视图的alpha它会淡入。更改代码来执行此操作会看起来像这样。

UIImageView *prevImageView = [self getImageViewWithIndex:currentIndex];
UIImageView *nowImageView = [self getImageViewWithIndex:newIndex];

currentIndex = newIndex;
[slideShowView addSubview:nowImageView];
nowImageView.alpha = 0.0;
[UIView animateWithDuration:4 
                 animations:^
    {
        nowImageView.alpha = 1.0;
    } 
                completion:^(BOOL finished) 
    {
        [prevImageView removeFromSuperview];
    }];

答案 1 :(得分:2)

[UIView transitionWithView:slideShowView 
                  duration:4.0 
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{
                    [prevImagesView removeFromSuperview];
                    [slideShowView addSubview:nowImageView];
                }
                completion:nil];

在这种情况下,slideShowView是prevImagesView和nowImagesView的超级视图,充当容器视图。

答案 2 :(得分:-1)

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];

//you can choose type of animation here 

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

[prevImageView removeFromSuperview]; 
[slideShowView addSubview:nowImageView]; 

[UIView commitAnimations]; // commit your animation

希望这会对你有所帮助