代码之家  ›  专栏  ›  技术社区  ›  Asad Khan

检测iPhone上的长按

  •  6
  • Asad Khan  · 技术社区  · 16 年前

    我正在开发一个iPhone应用程序,它要求我检查按钮是否被按下了6秒钟,然后启动一个正在播放某种声音的动作。

    我该如何检测这6秒的点击?

    另一方面,用户还可以继续点击按钮6秒钟,然后触发相同的操作。

    我应该如何处理多个水龙头,我怎么知道所有水龙头都在6秒的范围内?

    3 回复  |  直到 16 年前
        1
  •  17
  •   codingminion outis    11 年前

    对于6秒钟长的按压,使用 UILongPressGestureRecognizer 用它 minimumPressDuration 属性设置为6。

    自己写 gesture recognizer (比如说, LongTappingGestureRecognizer )在一段时间内连续敲击;不应该太棘手。给它一个像 uilongpressgesturerecognizer公司 的 最小压力持续时间 (比如说, minimumTappingDuration )和财产(比如, maximumLiftTime )这就决定了一根手指能被举起多长时间,然后它就不会被认为是一个长的敲击动作了。

    当手势结束后,有多种处理识别的策略。 最小抽头持续时间 . 一个是登记两个 触摸屏:带事件: 和 触摸已设置:WithEvent: 如果当前时间和开始时间之间的差异为>,则处理程序= 最小抽头持续时间 . 问题是它的时间比 最小抽头持续时间 如果用户缓慢敲击,并且当 最小抽头持续时间 达到。另一种方法是启动另一个nstimer(识别计时器),当第一个 触摸屏:带事件: 收到,将导致转换到 recognized state 在年取消了 取消识别 . 这里的棘手问题是,如果计时器启动时手指被举起,该怎么做。最好的方法可能是两种方法的结合,如果手指被举起,则忽略识别计时器。

    还有更多的细节,但这就是要点。基本上,它是一个长按识别器,允许用户在短时间内将HIR手指抬离屏幕。您可能只使用轻敲识别器并跳过长按识别器。

        2
  •  11
  •   hris.to    13 年前

    我知道这是一个很过时的问题,但答案应该很简单。

    在视图控制器中 可视负载 :

    //create long press gesture recognizer(gestureHandler will be triggered after gesture is detected)
    UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
    //adjust time interval(floating value CFTimeInterval in seconds)
    [longPressGesture setMinimumPressDuration:6.0];
    //add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead)
    [self.m_pTable addGestureRecognizer:longPressGesture];
    [longPressGesture release];
    

    然后在你 妊娠子宫切除术 :

    -(void)gestureHandler:(UISwipeGestureRecognizer *)gesture
    {
        if(UIGestureRecognizerStateBegan == gesture.state)
        {//your code here
    
        /*uncomment this to get which exact row was long pressed
        CGPoint location = [gesture locationInView:self.m_pTable];
        NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/
        }
    }
    
        3
  •  0
  •   mert    14 年前

    这是我的解决方案。

    - (IBAction) micButtonTouchedDownAction {
        self.micButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(micButtonAction:) userInfo:nil repeats:YES];
        self.micButtonReleased = FALSE;
    }
    
    - (IBAction) micButtonTouchedUpInsideAction {
        self.micButtonReleased = TRUE;
    }
    
    - (IBAction) micButtonTouchedUpOutsideAction {
        self.micButtonReleased = TRUE;
    }
    
    - (void) micButtonAction:(NSTimer *)timer {
        [self.micButtonTimer invalidate];
        self.micButtonTimer = nil;
    
        if(self.micButtonReleased) {
            NSLog(@"Tapped");
        }
        else {
            NSLog(@"Touched");
        }
    }