Evgeniy Kleban
Evgeniy Kleban

Reputation: 6975

How to apply animation during switching images

I have an imageView in top of my screen, and what i want is - to switch images with specific time interval, and to make it with animation (I'm not specify what really animation should be, that could be fade for example). I have suggested to add that code snippet:

 // load all the frames of our animation
    self.imageSlideshow.animationImages = [NSArray arrayWithObjects:
                                      self.firstImageIps, self.secondImageIps, self.thirdImageIps, self.fourthImageIps, self.fifthImageIps, nil];

    // all frames will execute in 1.75 seconds
    self.imageSlideshow.animationDuration = 1.75;
    // repeat the animation forever
    self.imageSlideshow.animationRepeatCount = 0;
    // start animating
    NSLog(@"that one called?");
    [self.imageSlideshow startAnimating];

But images here only switching, without any animation. And here is more serious issues - if i set duration to more, then 3 seconds, it start act weird (not of all images show, first image show more often then second and third. It look like it can't achieve a full cycle and move to first image after some short time again and again).

How to achieve switching images with animation, with timer interval, for example 5-7 seconds?

Any advice would be appreciated, thanks!

Upvotes: 1

Views: 85

Answers (4)

Szu
Szu

Reputation: 2252

To make animation between 2 view you may use spacial UIView method:

- (void)makeCrossDissolveAnimation {
    NSArray *images = self.imageSlideshow.animationImages;
    static int counter = 0;
    UIImageView *startView = [[UIImageView alloc] initWithImage:images[counter]];
    counter = (counter+1) % [images count];
    UIImageView *endView = [[UIImageView alloc] initWithImage:images[counter]];
    [UIView transitionFromView:startView toView:endView duration:ELAnimationDuration
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                        // Your completion block
                    }];
}

Upvotes: 1

Deepika
Deepika

Reputation: 263

Try this one:

   - (void)viewDidLoad {
    [super viewDidLoad];
    self.imgArr = [[NSArray alloc] initWithObjects:@"DSC06715.JPG",@"Snapshot_20121113_18.JPG",@"IMAG1689.jpg",@"IMAG1720.jpg",@"IMAG1396.jpg", nil];

    [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(performTransition) userInfo:nil repeats:YES];

    }

    -(void)performTransition
    {
      int randImg = arc4random()%self.imgArr.count;
      [self.imageView1 setImage:[UIImage imageNamed:[self.imgArr objectAtIndex:randImg]]];

      CATransition *transition = [CATransition animation];
      transition.duration = 1;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

      NSString *types[4] = {kCATransitionFade};
      transition.type = types[1];
      transition.delegate = self;
      [self.imageView1.layer addAnimation:transition forKey:nil];
    }

Upvotes: 2

Ramesh Muthe
Ramesh Muthe

Reputation: 811

-(void)animateImages
{
    count++;

   [UIView transitionWithView:imageSlideshow
                      duration:2.0f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        imageSlideshow.image = [imagesArray objectAtIndex: count % [imagesArray count]];
                    } completion:^(BOOL finished) {
                        [self animateImages];
                    }];
}

This code snippet will help you to add any type of animation.

Upvotes: 1

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

One of the simplest ways to do animation.

Code example :

   [UIView animateWithDuration:0.3 animations:^{
                        //change alpha with animation.
                        self.friendsTableView.alpha = 1.f;
                    } completion:^(BOOL finished) {

                    }];

This animation could be set with delay and optionsas well. As far as i know it is the simplest way to set proper animation with less afford.

Upvotes: 1

Related Questions