views:

1123

answers:

3

I have a rather basic question. I have several(5) UIImageView images on my screen. When I click a button, I want to change each image to something else. I also want to play a sound as each image is changed.

So I basically want a clicking sound as each image changes.

The problem is, for some reason if I have say 5 images,
the sound gets played 5 times first and then the images change.

It's like the images only refresh when control goes back to the user for input.

How can I "force" it to refresh the images as soon as i tell it what image to display?

I'm new to the iphone/macworld, so go easy on me :-)

Rick

A: 

It sounds like you're probably loading the images off disk or network while playing a sound asynchronously. Perhaps you're using AudioServicesPlaySystemSound along with UIImage +imageWithContentsOfURL:? The actual image loading and sound playing code would be most useful here.

Rob Napier
A: 

It's like the images only refresh when control goes back to the user for input.

That's exactly what happens. The UI is updated on the main thread so no updates happen whilst your code is running.

In order to achieve what you want, you're going to need timers. Take a look at the NSTimer documentation. You need to create a timer that fires a method to change your second image and then requeue a timer to change your third image. There is a convenient method to create a timer like you need: + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

[NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateImage2:) userInfo:nil repeats:NO];

then, you have a method:

- (void) updateImage2(NSTimer*)theTimer
{

     //Update image 2 here
     // play sound 2 here

    [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateImage3:) userInfo:nil repeats:NO];
}

I'll leave it to you to implement the method updateImage3: ;-)

Running the updates like this gives the main thread a chance to update the UI.

Roger Nolan
So, how would someone accomplish this? (Seeing images display one after the other instead of all at once at the end) Would you have to drop into openGL? Seems like such a simple thing :-)
added to answer.
Roger Nolan
Note that like Ben's answer in the duplicate, you can also use -[NSObject performSelector:withObject:afterDelay:]
Roger Nolan
Roger, thanks a million! Your right I probably should have posted that sample here. I just wasn't sure if i should have made it a comment or answer my own question so I just started fresh :-) I really appreciate the help. I've been stuck on this for a day and a half! I come from a Windows/Delphi background and thought I could port one of my games to iphone. I had no delusions it would be easy, but it is proving much tougher than I thought it would be.Thanks again for you help!
A: 

Roger,

The NSTimers did the trick!

Thanks again for taking time to help. Rick

Glad to hear it. Please upvote me and mark my answer correct :-)
Roger Nolan