Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C & iOS: running a timer? NSTimer/Threads/NSDate/etc
    text
    copied!<p>I am working on my first iOS app, and have run in the first snag I have not been able to find a good answer for.</p> <p>The problem: I have a custom <code>UIGestureRecognizer</code> and have it all wired up correctly, and I can run code for each touch in the <code>@selector</code> after recognition. This has been fine for most things, but it's a little too much input for others. </p> <p>My goal: To make a timer that triggers at a specified interval to run the logic, and to be able to cancel this at the moment touches are cancelled.</p> <p>Why I am asking here: There are a lot of possibilities for solutions, but none has stood out as the best to implement. So far it seems like</p> <ul> <li><code>performSelector</code> (and some variations on this)</li> <li><code>NSThread</code></li> <li><code>NSTimer</code></li> <li><code>NSDate</code></li> <li>Operation Queues</li> <li>I think I found some others as well...</li> </ul> <p>From all the research, some form of making a thread seems the route to go, but I am at a loss at which would work best for this situation. </p> <p>An example of an implementation: an <code>NSPoint</code> is taken every 0.10 seconds, and the distance between the previous and current point is taken. [Taking the distance between every point was yielding very messy results]. </p> <p>The relevant code:</p> <pre><code>- (void)viewDidLoad { CUIVerticalSwipeHold *vSwipe = [[CUIVerticalSwipeHold alloc] initWithTarget:self action:@selector(touchHoldMove:)]; [self.view addGestureRecognizer:vSwipe]; [vSwipe requireGestureRecognizerToFail:doubleTap]; } ... - (IBAction)touchHoldMove:(UIGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { } if (sender.state == UIGestureRecognizerStateBegan) { } //other stuff to do goes here } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload