ios - 在iOS中的UIView上连续按下手势

标签 ios touch uigesturerecognizer

有什么方法可以让用户连续按下UIView来得到通知吗?即我一直用一根手指按UIView,并且我想在此期间一次又一次地继续调用特定方法。

我尝试过UILongPressGestureRecognizer,但它只是通知开始,结束,移动等。与TouchesBegan相同。

干杯

最佳答案

您可以做的是,使用UILongPressGestureRecognizer和NSTimer的组合功能。

以下代码应满足您的要求。

@property (strong, nonatomic) NSTimer *timer;

- (void) viewDidLoad

{

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]         initWithTarget:self action:@selector(longPress:)];

   [aView addGestureRecognizer: longPress];

}

- (void) longPress:(UILongPressGestureRecognizer*)gesture

{

      if ( gesture.state == UIGestureRecognizerStateBegan ) 

  {

        self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(callMethod) userInfo:nil repeats:YES];

        [self.timer fire];

 }
    if ( gesture.state == UIGestureRecognizerStateEnded )

    {

            [self.timer invalidate];

 }

}

- (void) callMethod

{

   //will be called continuously within certain time interval you have set

}

关于ios - 在iOS中的UIView上连续按下手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904265/

相关文章:

c# - InjectTouchInput Windows 8 C# 不工作(返回 false)

iphone - 当 Gesture 应用于 self.view 时,我们如何才能使 TableView 工作?

ios - 如何在清晰且不同的 uiview 可以处理它们时忽略最顶层 uiview 中的触摸事件

ios - AVAudioPlayer 丢失 "Playing"状态

ios - 如何使用 UIStepper

ios - reloadRowsAtIndexPaths 不更新我的单元格数据

php - Linux Change 文件的修改日期?

javascript - jQuery 列表上的 TouchSwipe

ios - 禁止自动布局警告

ios - 快速获取点击的 ImageView