Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 647
solenoid Avatar asked Dec 27 '11 02:12

solenoid


2 Answers

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.

like image 195
Hivebrain Avatar answered Nov 12 '22 08:11

Hivebrain


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];
     }
}
like image 26
solenoid Avatar answered Nov 12 '22 07:11

solenoid