0

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

    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;

但这没有帮助。

4

3 回答 3

5

添加或删除视图或设置 UIImageView 的图像在animateWithDuration:....

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];
    }];
于 2012-06-14T08:33:12.440 回答
2
[UIView transitionWithView:slideShowView 
                  duration:4.0 
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{
                    [prevImagesView removeFromSuperview];
                    [slideShowView addSubview:nowImageView];
                }
                completion:nil];

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

于 2012-06-14T08:41:23.407 回答
-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

希望对你有帮助

于 2012-06-14T08:21:27.797 回答