有沒有什麼辦法可以在UIView
上持續按下通知?即我用一根手指持續按UIView
,並且我想在該持續時間內一次又一次地繼續調用特定的方法。在iOS的UIView上持續按下手勢
我試過UILongPressGestureRecognizer
但它只是得到通知開始,結束,移動等與TouchesBegan
相同。
乾杯
有沒有什麼辦法可以在UIView
上持續按下通知?即我用一根手指持續按UIView
,並且我想在該持續時間內一次又一次地繼續調用特定的方法。在iOS的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
}
火NSTimer
在touchesBegan
打電話要選擇和touchesEnded
方法
無效!在TouchesBegan
啓動一個定時器,當你已經達到了期望的時間量進行選擇(長按)。 在TouchesEnded
使計時器無效以防止執行選擇器。
我還設置了一個額外的標誌檢測「fingerReleased」: 設置fingerReleased = NO
上TouchesBegan
和fingerReleased = YES
上TouchesEnded
並提出要在執行代碼:
if (!fingerReleased)
{
// Execute code
}
TouchesBegan and TouchesEnded:P – GenieWanted
@GenieWanted,編輯謝謝:-) –