开发者

How to replace TouchesBegan with UIGestureRecognizer

here's the problem: I'd like to move to using UIGestureRecognizer in my Apps. For this reason I'd like to ditch TouchBegan/TouchEnded event's from my views.

However I don't understand how to manage when the touch began (user puts its finger on the screen) with UIGestureRecognizers.

The simplest one is UITapGestureRecognizer but the selector associated gets fired only when the TapGesture is completed (Well... it makes completely sense of course). But still the problem remains: how can I stop using touchesBegan and get that event开发者_如何学JAVA anyway from UIGestureRecognizer?

Thanks!


Here is an example:

 //Pan gesture
recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
((UIPanGestureRecognizer *)recognizer).minimumNumberOfTouches = 3; //number of fingers
recognizer.delegate = self;
[self.view addGestureRecognizer:recognizer];

[recognizer release];

- (void)handlePan:(UIPanGestureRecognizer *)recognizer 
{
   if (recognizer.state == UIGestureRecognizerStateBegan)
   {
    //do something
   } else if (recognizer.state == UIGestureRecognizerStateEnded) 
   {
    //do something 
   }
}

Also implement UIGestureRecognizerDelegate in .h file. May be you need to do self.view.userInteractionEnabled = YES depending on the view you're using. e.g., if it's UIImageView, the you need to set userInteractionEnabled = YES, default is NO


For what you are tryin ti do you can't. The gesture recoginizers are for high level gestures so they behaive the same across all apps (think swipes, the timing required for a double tap, etc). For low level control and to do things that the recognizers can't you will still have to implement logic in touchesbegan, touchesEnded, etc.


Why not implement your own touchesBegan in a UIGestureRecognizer subclass -- intercept the message, extract the information you'd like, and then pass the message along to super's touchesBegan?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜