Kumar D
Kumar D

Reputation: 1378

IPhone - How to fire the event immediately

I am working on Iphone application and new to dev. I am sorry if my question is very basic one. I searched net, but could not get answer.

My question is, when user touches the iphone, I want to get that event to be exexecuted immediately instead of waiting for the previous event to complete. I call while loop(present in different class) inside touchBegan method. I want to stop that loop when I get another touch event. But touchBegan event is not called (instead queued) as I am still present in the touch began.

Can anybody please help me how to fire the touch event immediately instead of waiting for the previous event to finish.

Thank you in advance.

Upvotes: 0

Views: 377

Answers (1)

Kobski
Kobski

Reputation: 1636

On the iPhone, one event has to run to completion before the next event is processed. Basically you want to have code which does not run for a long time so that your app remains responsive to user input.

There are a number of way you can handle this.

  1. Put some of the code in a new thread and start that thread from your touchBegan method.
  2. Or turn the loop into multiple events on the main thread. You can do this by calling:

    - (void)initializeLoop
    {
      loopCounter = 0;
        [self performSelector:@selector(nextLoop) withObject:nil afterDelay:0.0];
    }
    
    - (void)nextLoop
    {
      loopCounter++;
      if (loopCounter<MaxCount)
        [self performSelector:@selector(nextLoop) withObject:nil afterDelay:0.0];
    }
    

That way the new touchEnded event can be handled between these other events. Setting the delay higher than 0.0 will make the app even more responsive.

Upvotes: 1

Related Questions