Search code examples
iosobjective-cnstimer

Switching images using NSTimer


I have couple of images i want to switch. There is a simple function i made for do this:

-(void)imageSlide{

if (!isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x - 320;
self.imageSlideshow.frame = frame;
NSLog(@"1 caze work");
isMoved = YES;
}

if (isMoved){
CGRect frame = self.imageSlideshow.frame;
frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
self.imageSlideshow.frame = frame;
NSLog(@"2 caze work");
isMoved = NO;
}
}

There is NSTimer which call that function:

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

BOOL isMoved; placed in implementation of class.

What i want is, to remove an image and then, shown again (and repeat it every 2 seconds). It would be nice to have smooth animation as well.

That code:

for (int i=0; i<99; i++){

        self.imageSlideshow.image = [UIImage imageNamed:(i % 2) ? @"ipd1.jpg" : @"ipd2.jpg"];

        CATransition *transition = [CATransition animation];
        transition.duration = 1.0f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type = kCATransitionFade;

        [self.imageSlideshow.layer addAnimation:transition forKey:nil];
    }

Also not working, no idea why. Image stand still. I did import quartz library and include headers.


Solution

  • you can acheive this by using this code :-

    #import <QuartzCore/QuartzCore.h>
    ...
    imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"];
    
    CATransition *transition = [CATransition animation];
    transition.duration = 2.0f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    
    [imageView.layer addAnimation:transition forKey:nil];
    

    and NSTimer Job should be done here by transition.duration. So, no need of NSTimer anymore here.

    Courtesy :- https://stackoverflow.com/a/2834693/1865424

    This piece of code works too :-

     // create the view that will execute our animation
     UIImageView* imageSlideshow = [[UIImageView alloc] initWithFrame:self.view.frame];
    
     // load all the frames of our animation
     imageSlideshow.animationImages = [NSArray arrayWithObjects:    
                                 [UIImage imageNamed:@“ipd1.jpg"],
                                 [UIImage imageNamed:@“ipd2.jpg"], nil];
    
     // all frames will execute in 1.75 seconds
     imageSlideshow.animationDuration = 1.75;
     // repeat the animation forever
     imageSlideshow.animationRepeatCount = 0;
     // start animating
     [imageSlideshow startAnimating];
     // add the animation view to the main window 
     [self.view addSubview:imageSlideshow];