ios中的手势识别器

时间:2012-01-18 10:20:21

标签: iphone ios uigesturerecognizer

我在LongTapGestureRecognizer中使用此代码来自动滚动视图:

-(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]; 
}

它对我来说很完美,但我需要的是,当用户第二次点击Longgesture的屏幕时,必须停止自动扫描,反之亦然。当用户再次点击时,如何停止此操作。

3 个答案:

答案 0 :(得分:0)

看起来你几乎就在那里。你可能想要这样的东西:

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;
    }
}

答案 1 :(得分:0)

我通常做的是声明全局BOOL Alter;并初始化它Alter = NO;在viewDidLoad(或任何其他方法)然后

-(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]; 
    }
}

答案 2 :(得分:0)

在viewDidLoad或类似的东西中创建名为shouldFireTimer的BOOL,并在每次检测到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]; 
}

或者像Matt所说,上面可能只是检查nil状态而不是使用BOOL。我建议使用BOOL,因为你可能在其他情况下(例如从按钮)启动autoscrollTimerFired,即当你想调用它时它可能不是nil。