Nareille
Nareille

Reputation: 821

Toggle between two images with sleeptime

I need to toggle a specific amount of times between two (or maybe later on more) pictures after a button was pressed, and wait a second or two for the change. When a stop-button is pressed at any time, the toggling should stop. My code by now looks like this

IBOutlet UIImageView *exerciseView;

- (void) repetitionCycle {

    stopButtonPressed = NO;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    for (NSInteger counter = kRepetitions; counter >=0; counter--) {
        exerciseView.image = [UIImage imageNamed:@"blue.jpg"];  
        [NSThread sleepForTimeInterval:kSleepDuration];                                                                 
        if (stopButtonPressed) {break;}     

        image = [UIImage imageNamed:kExerciseEndingPosition];                                                           
        exerciseView.image = [UIImage imageNamed:@"image1.jpg"];
        [NSThread sleepForTimeInterval:kSleepDuration];     

        if (stopButtonPressed) {break;}     
    }

    self.stopRepetitionCycle;
    [pool release];

}

exerciseView is Besides other things, in stopRepetitionCycle I just set stopButtonPressed to YES, so it stops after it finished the "for" for the first time. It does count down, it does stop after one cycle, but it doesn't change the picture.

While fiddling around, I set the initial picture via IB, so it finally displayed ANYTHING.. The fun part, when I hit the stop button in the right moment, the second picture is shown. So I guessed I need to set the view manually every time the image should toggle. But

        [self.exerciseView addSubview:image];

gives me the error

Incompatible Objective-C types "struct UIImage *", expected "struct UIView *" when passing argument 1 of "addSubview:" from distinct Objective-C type

Also

        [self.exerciseView.image addSubview:image];

doesn't do the job and gives me a

UIImage may not respond to addSubview

Any idea what I have to do here?

Thank you very much!

Upvotes: 0

Views: 443

Answers (4)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Use UIImageView. It has built-in support for this.

imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"blue.jpg"], [UIImage imageNamed:@"image1.jpg"], nil];
imageView.animationDuration = kSleepDuration * [imageView.animationImages count];

Wire this function to the start button

- (IBAction) startAnimation {
    [imageView startAnimating];
}

and this to the stop button

- (IBAction) stopAnimation {
    [imageView stopAnimating];
}

Upvotes: 0

HG's
HG's

Reputation: 828

The method addSubview is used to add only UIViews. You can't use it with a UIImage class. The general syntax is :

[(UIView) addSubview:(UIView*)];

replace

[self.exerciseView addSubview:image];  

with

self.exerciseView.image = image;  

For the same reason

[self.exerciseView.image addSubview:image];  

won't work either.

Upvotes: 0

sergio
sergio

Reputation: 69027

uuhm...

your usage of [NSThread sleep...] puzzles my...

in fact: if you are on a secondary thread (meaning, not the main thread), then you are doing something not allowed, which is trying to access the UI from a secondary thread.

this could explain the strange behavior you are seeing.

on the other hand, if this is the main thread, possibly is not a good idea to call sleep... because that way you will freeze entirely the UI, and you could not possibly intercept the click on the button...

anyhow, what I would suggest, is using NSTimers to move from one image to the next one at certain intervals of time. when the stop button is hidden you would simply cancel the timer and the slideshow would end. pretty clean.

as to the error messages you are having with your image, the fact is that UIImage is not a UIView, so you cannot add it as a subview, but this is not what does not work here...

Upvotes: 1

hwaxxer
hwaxxer

Reputation: 3383

Updating the UI within a loop is almost guaranteed to fail. You should most likely be using an NSTimer to swap the image at a given interval instead.

Furthermore, [self.exerciseView addSubview:image] is failing because you're passing a UIImage and not a UIView. Create a UIImageView (which is a subclass of UIView) with your UIImage and pass that instead.

Upvotes: 0

Related Questions