繁体   English   中英

iOS中的手势识别器

[英]Gesture recognizer in ios

我在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屏幕时,自动勾画必须停止,反之亦然。 当用户再次点击时如何停止。

看来您快要到了。 您可能想要这样的东西:

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

我通常要做的是声明一个全局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]; 
    }
}

在viewDidLoad或类似文件中创建一个名为shouldFireTimer的BOOL,并在每次检测到长按时更新其值

-(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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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