本文介绍了实现按住连续事件触发的优雅方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要通过按住按钮来触发一系列事件.想想一个增加字段的 + 按钮:点击它应该增加 1,但是点击 &保持应该说每秒增加 1 直到按钮被释放.另一个例子是在音频播放器类型应用中按住后退或前进按钮时的擦洗功能.

I often have a need to fire a sequence of events as a result of holding down a button. Think of a + button that increments a field: tapping it should increment it by 1, but tap & hold should say increment this by 1 every second until the button is released. Another example is the scrubbing function when holding down the backwards or forwards button in an audio player type app.

我通常采用以下策略:

  1. touchDownInside 上,我设置了一个重复计时器,时间间隔是我想要的.
  2. touchUpInside 上,我使计时器无效并释放.
  1. On touchDownInside I set up a repeating timer with my desired interval.
  2. On touchUpInside I invalidate and release the timer.

但是对于每个这样的按钮,我需要一个单独的计时器实例变量、2 个目标操作和 2 个方法实现.(这是假设我正在编写一个通用类并且不想对最大同时触摸数施加限制).

But for every such button I need a separate timer instance variable, and 2 target-actions, and 2 method implementations. (This is assuming I'm writing a generic class and don't want to impose restrictions on the max number of simultaneous touches).

有没有更优雅的方法来解决我缺少的这个问题?

Is there a more elegant way to solve this that I'm missing?

推荐答案

我建议 UILongPressGestureRecognizer.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addOpenInService:)];
    longPress.delegate      =   self;
    longPress.minimumPressDuration = 0.7;
    [aView addGestureRecognizer:longPress];
    [longPress release];
    longPress = nil;

在触发事件时,您可以接到电话

On firing the event, you can get the call in

- (void) addOpenInService: (UILongPressGestureRecognizer *) objRecognizer
{
    // Do Something
}

同样,您可以使用 UITapGestureRecognizer 来识别用户点击.

Likewise you can use UITapGestureRecognizer for recognizing user taps.

希望这会有所帮助.:)

Hope this helps. :)

这篇关于实现按住连续事件触发的优雅方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 14:58