2014-02-20 47 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 
}