Objective C & iOS: running a timer? NSTimer/Threads/NSDate/etc
Asked Answered
P

2

6

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.

The problem: I have a custom UIGestureRecognizer and have it all wired up correctly, and I can run code for each touch in the @selector after recognition. This has been fine for most things, but it's a little too much input for others.

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.

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

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

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.

An example of an implementation: an NSPoint 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].

The 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

}
Perversity answered 27/12, 2011 at 2:48 Comment(0)
S
11

Use an 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 cancel it, do something like this:

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

Note that in the above example you would need to declare the "theTimer" instance of NSTimer where it will be available to both methods. In the above example the "0.5" means that the timer will fire twice a second. Adjust as needed.

Segregationist answered 27/12, 2011 at 4:50 Comment(1)
Note: It is considered good practice to set the timer to nil after invalidating it: [theTimer invalidate]; theTimer = nil;Dyestuff
P
1

For the sake of completeness, I am adding my final implementation here (not sure this is the way to do it, 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];
     }
}
Perversity answered 3/1, 2012 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.