iPad 应用程序 UIButton 的响应时间

发布于 2024-09-06 03:43:12 字数 289 浏览 4 评论 0原文

我有一个简单的 UIButton,单击后会播放 1 秒的声音。我希望能够非常快速地单击该按钮并尽可能多次地发出该声音。

我目前已经通过包括来启动并运行它,也许这就是罪魁祸首......另外,我正在深入研究苹果的参考文献,但无法找到有关 UIButton 响应每个事件的速度有多快以及如何响应的信息,如果在所有,我可以控制和操纵这个值。

我应该切换到不同的音频框架,例如“音频工具箱”,还是有办法让我加快速度,或者也许指示一个按钮在第一次按下的操作仍在进行时接受第二次和第三次按下。

干杯!

〜尼尔。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2024-09-13 03:43:12

创建一个独立于按钮处理程序方法来播放声音的方法。假设您将其命名为 playSound。在按钮处理程序中,在后台执行该方法:

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

在播放声音之前,这确实会产生额外的开销来生成线程。如果您想加快速度,请创建一个工作线程池并使用:

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

创建工作线程池:(

-(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];
}

此代码未经测试,可能需要一些调试,但它应该让您朝着正确的方向前进。 )

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文