2014-02-20 42 views
1

有没有什么办法可以在UIView上持续按下通知?即我用一根手指持续按UIView,并且我想在该持续时间内一次又一次地继续调用特定的方法。在iOS的UIView上持续按下手势

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

干杯

回答

0

你可以做的是,使用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 

} 
2

NSTimertouchesBegan打电话要选择和touchesEnded方法

+0

TouchesBegan and TouchesEnded:P – GenieWanted

+0

@GenieWanted,编辑谢谢:-) –

2

无效!在TouchesBegan启动一个定时器,当你已经达到了期望的时间量进行选择(长按)。 在TouchesEnded使计时器无效以防止执行选择器。

我还设置了一个额外的标志检测“fingerReleased”: 设置fingerReleased = NOTouchesBeganfingerReleased = YESTouchesEnded并提出要在执行代码:

if (!fingerReleased) 
{ 
    // Execute code 
}