2011-05-15 6 views
0

私はiOSのタッチイベントとジェスチャーでかなり新しいです。iOSジェスチャーの問題 - 特定の時間間隔で接触し続けているタッチイベントを検出するにはどうすればよいですか?

ユーザーが3秒間といった一定の時間間隔で指を触れたままにすると、イベントをトリガーする必要があります。この種の長いプレスイベントを監視するにはどうすればいいですか?

参考になるコードを教えていただけたら幸いです。

ありがとうございました。

答えて

2

このようにすることができます。

NSTimer *touchesHoldTimer; 

そして:

@property (nonatomic, retain) NSTimer *touchesHoldTimer; 

- (void)touchesHoldCheckTime; 

を合成して、.mファイルでtouchesHoldTimer

を解放することを忘れないでください:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *countTouches = [event allTouches]; 

    NSLog(@"touchesBegan"); 

    if ([countTouches count] == 1) { // Not multitouch 
     NSLog(@"Starting timer.."); 

     touchesHoldTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(touchesHoldCheckTime) userInfo:nil repeats:NO]; 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"touchesEnded"); 

    if (touchesHoldTimer != nil) { 
     [touchesHoldTimer invalidate]; 
     touchesHoldTimer = nil; 
    } 
} 

- (void)touchesHoldCheckTime { 
    NSLog(@"You have hold me down for 3 sec."); 

    [touchesHoldTimer invalidate]; 
    touchesHoldTimer = nil; 
} 
2

あなた.hファイルで

お使いのアプリケーションのインターフェイスに合わせてUILongPressGestureRecognizerが適しているかどうかを調べることをお勧めします。上のリンゴのドキュメントには、サンプルコードリンクがあります。 UIViewは、オーバーライドする4つのメソッドのみを提供します。ジェスチャーレコグナイザーでもう少しやり直すことができます。 Here's他のジェスチャ認識プログラムのサンプルコード。お役に立てれば。

関連する問題