-1

Sorry this is a repeat question, but I have tried all the solutions I could find without success.

Anyway, here is my problem, specifically. I have the following code where I start a timer on a long press gesture, then stop it when that gesture is cancelled or when the action is complete.

-(void)photoLongPress:(UILongPressGestureRecognizer *)press
{
    NSTimer *timer = nil;

    if (press.state == UIGestureRecognizerStateBegan)
    {
        //Start timer
        timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(photoLongPressTimer:) userInfo:nil repeats:YES];
    }
    if (press.state == UIGestureRecognizerStateEnded)
    {
        [timer invalidate];
        timer = nil;
    }
}

-(void)photoLongPressTimer:(NSTimer *)timer
{
    DAProgressOverlayView *progress = (DAProgressOverlayView *)[self.view viewWithTag:kTagPhotoDeleteProgressIndicator];

    progress.progress += 0.08;

    if (progress.progress == 1)
    {
        [self deletePhotoSelection];
        [timer invalidate];
        timer = nil;
    }
}

The timer stops when invalidated in the photoLongPressTimer method, but not if the gesture stops in the photoLongPress method. Why might this be? I'm stumped.

Thanks.

3 Answers 3

3

You need to store timer in an instance variable so that you can refer to it later. It works in photoLongPressTimer: because the timer is passed as a parameter but in photoLongPress: it is nil because you haven't just created it and it's a local reference.

Add a property:

@property (strong, nonatomic) NSTimer *timer;

Store the new timer into it:

self.timer = [NSTimer schedu...

Invalidate and nil the property:

[self.timer invalidate];
self.timer = nil;
2
  • This works great. I am curious though, why would a local variable not work when there (at leafs at first appearence) seems to be a reference to the original object? Aug 20, 2013 at 12:00
  • 1
    You have the reference when you initially create the timer but each time the gesture changes the method is called again a new local reference is created (and set to nil).
    – Wain
    Aug 20, 2013 at 12:02
2
NSTimer *timer = nil;

This line only creates a local variable. You must make a property for the timer. Put this in your .h file.

@property (strong) NSTimer *timer;

Then use the code here

-(void)photoLongPress:(UILongPressGestureRecognizer *)press
{    
    if (press.state == UIGestureRecognizerStateBegan)
    {
        //Start timer
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(photoLongPressTimer:) userInfo:nil repeats:YES];
    }
    if (press.state == UIGestureRecognizerStateEnded)
    {
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)photoLongPressTimer:(NSTimer *)timer
{
    DAProgressOverlayView *progress = (DAProgressOverlayView *)[self.view viewWithTag:kTagPhotoDeleteProgressIndicator];

    progress.progress += 0.08;

    if (progress.progress == 1)
    {
        [self deletePhotoSelection];
        [timer invalidate];
        timer = nil;
    }
}
1

Problem is with NSTimer *timer = nil; in -(void)photoLongPress:(UILongPressGestureRecognizer *)press method.

remove NSTimer *timer = nil; from this method and store instance of NSTimer.

1
  • Would be helpful to elaborate on which manner to store the instance since technically the poster is storing an instance, albeit temporarily.
    – Jeremy
    Aug 20, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.