Question

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

Was it helpful?

Solution

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.

OTHER TIPS

If you are looking for a way to refresh the UIImageView periodically, you can reference the following: http://www.shanison.com/2010/12/25/uiimageview-load-remote-image/

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.

This worked for me, when I wanted to change uiimageview background and refresh it, and continue executing code after that:

-(void)imgTapped_event:(UIImageView*)imgViewTapped
{
     imgViewTapped.backgroundColor=[UIColor whiteColor];

     //let the system update ui changes
     [NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.0f]];

     //continue your code execution, play sounds etc...

     //my helper method to play sound
     [self playSound:@"goodAnswer" sync:YES loops:0]; 
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top