C & iOS goal: starting a timer? NSTimer / Thread / NSDate / etc

I am working on my first iOS application and launched the first error, I could not find a good answer.

Problem: I have a custom UIGestureRecognizer and it is all connected correctly, and I can run the code for every click in @selector after recognition. It was good for most things, but a little too much for others.

My goal: to make a timer that starts at a given interval to start the logic, and be able to cancel it at the time of canceling orders.

Why I ask here: there are many opportunities for solutions, but none of them stand out best. So far it’s like

  • performSelector (and some variations of this)
  • NSThread
  • NSTimer
  • NSDate
  • Operation Queues
  • I think I found others ...

Of all the studies, some form of threading seems like a route for transition, but I'm at a loss what works best for this situation.

Implementation example: a NSPoint is taken every 0.10 seconds, and the distance between the previous and current point is taken. [Taking the distance between each point yielded very dirty results].

Relevant 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 } 
+6
source share
2 answers

Use NSTimer

Set it up like this:

  theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(yourMethodThatYouWantRunEachTimeTheTimerFires) userInfo:nil repeats:YES]; 

Then, when you want to undo it, do something like this:

  if ([theTimer isValid]) { [theTimer invalidate]; } 

Note that in the example above, you need to declare an instance of "theTimer" NSTimer, where it will be available for both methods. In the above example, β€œ0.5” means that the timer will fire twice a second. Adjust if necessary.

+11
source

For completeness, I add my latest implementation here (not sure if this is the way to do this, but here goes)

.h

 @interface { NSTimer *myTimer; } @property (nonatomic, retain) NSTimer *myTimer; 

.m

 @synthesize myTimer; ------------------------------------------- - (void)viewDidLoad { //Relevant snipet CUIVerticalSwipeHold *vSwipe = [[CUIVerticalSwipeHold alloc] initWithTarget:self action:@selector(touchHoldMove:)]; [self.view addGestureRecognizer:vSwipe]; [vSwipe requireGestureRecognizerToFail:doubleTap]; } ------------------------------------------- - (IBAction)touchHoldMove:(UIGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { //Cancel the timer when the gesture ends if ([myTimer isValid]) { [myTimer invalidate]; } } } if (sender.state == UIGestureRecognizerStateBegan) { //starting the timer when the gesture begins myTimer = [NSTimer scheduledTimerWithTimeInterval:someTimeIncrement target:self selector:@selector(someSelector) userInfo:nil repeats:YES]; } } 
+1
source

Source: https://habr.com/ru/post/904511/


All Articles