سؤال

Another quick one:

If I perform something like this:

runningAnimation = YES;
[self performSelector:@selector(animationsComplete) withObject:nil afterDelay:0.1[;

// Return to main function
-(void) mainFunction
{
while (runningAnimation)
{
continue;
}
}

// And, animationsComplete looks like this:
-(void) animationsComplete
{
runningAnimation = NO;
}

the Program never seems to get out of the while loop. Can anyone tell me why this is?

On another note, if this type of "wait in my code for something to finish executing" can't really ever work in the sense that I was trying to do, is there another way to do the same thing? To just pause in a function whilst waiting for a call of

[self performSelector:withObject:afterDelay:]

to complete? Sorry if this seems like an amateur question. I'm an amateur.

هل كانت مفيدة؟

المحلول

That's because your "performSelector:" is happening on your current (main) thread.

If you add a line like "NSLog( @"in main function");" inside that "while" loop, as soon as your performSelector fires up, it should stop.

You need to do the performSelector on another (detached) thread, or use a block. Some blocks can use "stop" Booleans in their arguments.

نصائح أخرى

WHILE (ANIMATIONCOMPLETE != 1) {
... Do work ...
... When work is done, like using if code... ...
IF (SomeBoolean // That means work done == (0 for false, continue, 1 for true, break)) {
BREAK;
}
ELSE {
CONTINUE
}
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top