我正在开发一个音乐应用程序,所以我不能真的批准任何延迟。
我正在使用心爱的触摸using /Moved/Ended来处理我的触摸。一切都很顺利,我设法合成了一个音调(使用AudioUnit),并在手指下显示了一个辉光(使用GLKit),如果同时敲击的音符/触摸少于4-7次,一切都很好,然后它就会变得疯狂,让应用程序卡住。
我理解这是因为我正在做很多工作( GLKit接口和我为我的synth引擎制作的接口),我需要一种方法来修复它。
我的代码是这样构建的:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *touch in touches)
{
CGPoint lePoint = [touch locationInView:self.view];
doing some calculataions for synth engine.......
actually making the sound avilable to play through the interface........
then I start to create the sprite object for the glow
attaching it to the nsarray of the shapes
//rejoycing in the awesomness of the sound
adding the touch to the active touches array
}
}我在touchesEnded中做了完全相反的操作。
因此,在我尝试让它更好地工作时,我尝试使用GCD来处理GLKit的东西,这样它就会异步发生,它起作用了,但有时我看到屏幕上有一个发光,因为当touchesEnded试图删除它时,它不在数组中。
所以这不管用,而且我有点糊涂了,如果有人能帮助我,我会很感激的。
发布于 2012-05-19 06:51:44
使用GCD,但要确保使用并发队列,以便可以同时提交所有的触摸。如果你需要等待它们的完成,使用dispatch_apply,否则就异步地踢开它们。
一旦计划了块,就不能取消它,因此您可能希望检查触摸是否已结束或取消,并立即返回块。
例如,像这样的东西。
for (UITouch *touch in touches) {
NSValue *key = [NSValue valueWithPointer:(__bridge void*)touch];
MyData *data = [dictionary objectForKey:key];
// set whatever attributes you want in the data for this touch
// You may want to have a cancel flag in there, so you can set it later...
CGPoint touchPoint = [touch locationInView:self.view];
dispatch_async(someConcurrentQueue, ^{
// If only one thing to do, check the flag at beginning, if it is a long task
// may want to check it periodically so you cancel as soon as possible.
if (data.touchHasEnded) return;
});另一种选择是每次触摸都有一个队列。您可以在touchesBegan中创建分派队列,然后在其中抛出任务。您可以让它们全部完成,或者也许其中一些应该在touchesEnd中取消。
例如,如果状态已经更改为其他状态,则处理中间的剩余触摸可能没有意义。
我做过这样的线条平滑。
无论哪种方式,如果您正在进行密集型处理,您都应该让系统找出如何管理它。只要确保你可以在适当的时候终止你正在做的事情。
确保将状态标记为完成,并且在touchesEnd之后不再处理任何其他内容...毕竟,您只是使用指针作为字典的关键字……
https://stackoverflow.com/questions/10652703
复制相似问题