简体   繁体   中英

Gesture recognizer in ios

I have this code in LongTapGestureRecognizer for autoscrolling a view:

-(void) longPressDetectedgesture:
        (UILongPressGestureRecognizer*)recognizer
{
    _btnautoscrollstop.hidden = NO;
    _btnautoscroll.hidden = YES;

    // if (autoscrollTimer == nil) { 

    autoscrollTimer = [NSTimer 
        scheduledTimerWithTimeInterval:(55.0/1000.0) 
        target:self 
        selector:@selector(autoscrollTimerFired:)  
        userInfo:nil  
        repeats:YES]; 
}
- (void)autoscrollTimerFired:(NSTimer*)timer { 
    CGPoint scrollPoint = self.table.contentOffset; 
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); 
    [self.table setContentOffset:scrollPoint animated:NO]; 
}

It works perfect for me, but my need is, the autoscrooling must stop when the user taps the screen for Longgesture for the second time and vise versa. How to stop this, when the user taps for a second time.

It looks like you're almost there. You probably want something like this:

if (recogniser.state == UIGestureRecognizerStateBegan) {
    if (autoscrollTimer == nil) { 
        autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(55.0/1000.0) 
                                                           target:self 
                                                         selector:@selector(autoscrollTimerFired:)  
                                                         userInfo:nil  
                                                          repeats:YES];
    } else {
        [autoscrollTimer invalidate];
        autoscrollTimer = nil;
    }
}

What i usually do is declare a global BOOL Alter; and initialize it Alter = NO; in viewDidLoad (or any other method) then

-(void) longPressDetectedgesture:(UILongPressGestureRecognizer*)recognizer
{
    if(Alter)
    {
      Alter = NO;
      [autoscrollTimer inValidate];
    }
    else
    {
       Alter = YES;
       _btnautoscrollstop.hidden = NO;
       _btnautoscroll.hidden = YES;

     // if (autoscrollTimer == nil) { 

     autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:(55.0/1000.0) 
                                                   target:self 
                                               selector:@selector(autoscrollTimerFired:)  
                                                 userInfo:nil  
                                                  repeats:YES]; 
    }
}

create a BOOL called shouldFireTimer in viewDidLoad or similar and update it's value each time you detect a longpress

-(void) longPressDetectedgesture: (UILongPressGestureRecognizer*)recognizer {
    _btnautoscrollstop.hidden = NO;
    _btnautoscroll.hidden = YES;
    if ( shouldFireTimer ) {
        [autoscrollTimer invalidate];
        autoscrollTimer = nil;
    } else {
        autoscrollTimer = [NSTimer 
            scheduledTimerWithTimeInterval:(55.0/1000.0) 
            target:self 
            selector:@selector(autoscrollTimerFired:)  
            userInfo:nil  
            repeats:YES]; 
    }
    shouldFireTimer = !shouldFireTimer;
}

- (void)autoscrollTimerFired:(NSTimer*)timer { 
     CGPoint scrollPoint = self.table.contentOffset; 
     scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1); 
     [self.table setContentOffset:scrollPoint animated:NO]; 
}

or like Matt says above maybe just check nil status instead of using a BOOL. I suggest using a BOOL as you maybe firing autoscrollTimerFired in other circumstances too (from a button for example) ie it might not be nil when you want to call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM