开发者

Response time for a UIButton for an iPad application

开发者 https://www.devze.com 2023-01-04 10:07 出处:网络
i have a simple UIButton that, once clicked, plays a 1 second sound. i want to be able to click that button really fast and produce that sound as many times as i humanly can.

i have a simple UIButton that, once clicked, plays a 1 second sound. i want to be able to click that button really fast and produce that sound as many times as i humanly can.

i currently have this up and running by including the and maybe that is where the culprit is... also, i am digging into apple's references and cannot find the info for how quick is a UIButton to respond to each event and how, if开发者_C百科 at all, i can control and manipulate this value.

should i switch to a different audio framework like the "audio toolbox" or is there a way for me to speed things up, or perhaps instruct a button to accept a 2nd and 3rd press while the action of the first press is still underway.

cheers!

~nir.


Create a method to play the sound separate from the button handler method. Let's say you call it playSound. In your button handler, execute that method in the background with:

[self performSelectorInBackground:@selector(playSound) withObject:nil];

That does incur additional overhead to spawn off a thread before it can play the sound. If you want to speed it up a bit more, create a pool of worker threads and use:

[self performSelector:@selector(playSound) 
             onThread:nextThread 
           withObject:nil 
        waitUntilDone:NO];

To create the pool of worker threads:

-(void)stillWorking {
    NSLog(@"Still working!");
}

-(void)workerThreadMain {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    NSTimer *threadTimer = [NSTimer scheduledTimerWithTimeInterval:10 
                                                            target:self 
                                                          selector:@selector(stillWorking) 
                                                          userInfo:nil 
                                                           repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:threadTimer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];

    [pool drain];
}

-(NSArray*)createWorkerThreads { 
    NSMutableArray *threadArray = [[[NSMutableArray alloc] initWithCapacity:10]autorelease];
    NSThread *workerThread;

    for (i=0;i<10;i++) {
        workerThread = [[NSThread alloc]initWithTarget:self 
                                              selector:@selector(workerThreadMain) 
                                                object:nil];
        [workerThread start];
        [threadArray addObject:workerThread];
    }
    return [NSArray arrayWithArray:threadArray];
}

(This code is untested and may require some debugging, but it should get you going in the right direction.)

0

精彩评论

暂无评论...
验证码 换一张
取 消