0

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.

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • http://stackoverflow.com/questions/7638831/fade-dissolve-when-changing-uiimageviews-image/38350024#38350024 – Kumar KL Jul 13 '16 at 11:27

4 Answers4

3

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];
Community
  • 1
  • 1
Kundan
  • 3,084
  • 2
  • 28
  • 65
  • Edward, please take a look at my updated answer, i wonder why its not work. Thanks. – Evgeniy Kleban Oct 01 '14 at 09:36
  • @EvgeniyKleban i edited my answer with the simpler version of code, check that..it works like a charm :) – Kundan Oct 01 '14 at 11:12
  • Thank you Edward for your effort. It work but without animation, just switching. At least that work, I'm happy now :) Btw, what is that mysterios animationImages property? – Evgeniy Kleban Oct 06 '14 at 09:07
  • Check this link https://developer.apple.com/library/ios/documentation/uikit/reference/uiimageview_class/index.html#//apple_ref/occ/instp/UIImageView/animationImages , here they have explained animationImages.. – Kundan Oct 06 '14 at 10:58
2

I think you need:

[UIView animateWithDuration:1 options:UIViewAnimationOptionCurveEaseIn 
   animations:^{
      //Change frame here.
   } completion:^ (BOOL completed) {}         
];
1

Try this code. It is worked for me.

#pragma mark -
#pragma mark viewDidAppear
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    updateBCK = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    [updateBCK fire];

}

#pragma mark -
#pragma mark viewDidDisAppear
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if ([updateBCK isValid]){
        [updateBCK invalidate];
        updateBCK = nil;
    }
}
-(void)changeImage{
    static int i=0;
    if (i == [myImages count]){
        i=0;
    }
    [UIImageView beginAnimations:nil context:NULL];
    [UIImageView setAnimationDuration:0.6];
    backgroundImageView.alpha=1; 
    backgroundImageView.image =[myImages objectAtIndex:i];

    [UIImageView commitAnimations];
    i++;
}
ssmanohar
  • 186
  • 1
  • 9
1

It is bug in your code. After setting isMoved = YES, you can't check if isMoved == YES.

I don't know if that is what you mean, but try this code:

-(void)imageSlide{

    [UIView animateWithDuration:1 animations:^{
        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;
        }
        else
        {
            CGRect frame = self.imageSlideshow.frame;
            frame.origin.x = self.imageSlideshow.frame.origin.x + 320;
            self.imageSlideshow.frame = frame;
            NSLog(@"2 caze work");
            isMoved = NO;
        }

    } completion:^(BOOL finished) {
    }];
}

EDIT Maybe this code will help you:

-(void)slideshow
{
    static int i = 0;
    UIImage * img = [UIImage imageNamed:(i % 2) ? @"ipd1.jpg" : @"ipd2.jpg"];

    [UIView transitionWithView:self.imageSlideshow duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.imageSlideshow.image = img;
    } completion:^(BOOL finished) {
        i++;
        [self performSelector:@selector(slideshow) withObject:nil afterDelay:2.0];
    }];
}
pawel_d
  • 487
  • 5
  • 9
  • Pawel, thank you for your help but there is no bug in checking condition (because i can see output in NSLog). – Evgeniy Kleban Oct 01 '14 at 06:47
  • @EvgeniyKleban In your code are executed both statements "if (!isMoved)" and "if (isMoved)" at the same time. Is this what you expect? – pawel_d Oct 01 '14 at 06:54
  • Pawel, yes, because boolean value declared in implementation file and therefore, every time function is called different statement executed. In my code it happen every 2 seconds. – Evgeniy Kleban Oct 01 '14 at 07:54
  • @EvgeniyKleban No it not work that way. Your function execute statement "if (isMoved)" at the first time. Each next calling this function executing both statements. "if (!isMoved)" and "if (isMoved)". Try to put breakpoint and debug your code. – pawel_d Oct 01 '14 at 08:05