ios 中的手势识别器

发布于 2024-12-27 12:32:53 字数 780 浏览 0 评论 0原文

我在 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]; 
}

它对我来说非常适合,但我的需要是,当用户第二次点击长手势屏幕时,自动滚动必须停止,反之亦然。当用户第二次点击时如何停止这种情况。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2025-01-03 12:32:53

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

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

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;
    }
}
夏末的微笑 2025-01-03 12:32:53

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

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]; 
    }
}
笙痞 2025-01-03 12:32:53

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

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) i.e. it might not be nil when you want to call it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文