views:

316

answers:

2

I'm hoping someone can steer me in the right direction.

I need to be able to update images on the screen one at a time after a button is pressed. It appears that they only get updated at the end of the cycle which is not how I need it to work.

I created a little example to show what I am doing. Basically I have 3 images on the screen. When the button is pressed I want to update the first image, play a sound, wait a second and the update the next etc.

However, I just hear 1 sound and all the images update at the end.

Seems like a simple thing. How should I be doing it?

#import "testappViewController.h"

@implementation testappViewController


@synthesize p1,p2,p3;   //UIImageViews
@synthesize button1;
@synthesize volleyFileID;

- (IBAction)buttonpressed:(id)sender
{
    NSString *TLS = @"RQP.png";

    NSString *volleyPath = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"caf"];
    CFURLRef volleyURL = (CFURLRef ) [NSURL fileURLWithPath:volleyPath];
    AudioServicesCreateSystemSoundID (volleyURL, &volleyFileID);

    TLS = @"RQP.png"; 
    UIImage *sampleimage= [[UIImage imageNamed:TLS] retain];
    p1.image = sampleimage; 
    AudioServicesPlaySystemSound(volleyFileID);

    [NSThread sleepForTimeInterval:0.5];

    p2.image = sampleimage; 
    AudioServicesPlaySystemSound(volleyFileID);

    [NSThread sleepForTimeInterval:0.5];

    p2.image = sampleimage; 
    AudioServicesPlaySystemSound(volleyFileID);

}
+1  A: 

You shouldn't be using -[NSThread sleepForTimeInterval:] here. Try using -[NSObject performSelector:withObject:afterDelay:], and your latter two image changes into a separate method. Then your event loop will run as normal, your sounds will play, and your entire app will remain responsive while it's going on.

Ben Gottlieb