UIView animation issue, when try no animatate images

I have to show slideshow of images. But at one moment I know only current and previous images (because of memory management). I need to show the next image with animation (user can shoose the animation type). But I see no animation, just new image appears without animation. Here is my code:

    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];
    }];

I've tried different animation options. Tried to set new image to existing image view

nowImageView.image = newImage;

But that didn't help.

Answers


Adding or removing a view or setting the image of an UIImageView is not animatable within animateWithDuration:....

With transitionWithView:...

What you were probably trying to do is to use an UIView-transition (since you specified a transition option). Then you should use

transitionWithView:duration:options:animations:completion:

instead, like this:

[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.

With animateWithDuraion:...

If you really wanted to use animateWithDuraion:... (maybe there are other animations happening at the same time) then you would have to animate some other property like the alpha of the view you are adding to have it fade in. Changing your code to do this would look something like this.

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];
    }];

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

In this case, slideShowView is the superview of both prevImagesView and nowImagesView, acting as a container view.


[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

hope this will help you


Need Your Help

What's the difference between quantize() and str.format()?

python django string quantization

I don't mean what's the technical difference, but rather, what's the faster/more logical or Pythonic, etc. way to do this: