How to handle single vs double touches on iPhone
I want to do something similar to iPhone's photo viewer. When you single tap, the UI bars disappear after a short delay. When you double tap, it zooms in. But I don't want anything to happen on double tap because I haven't yet implemente开发者_StackOverflow中文版d zooming in.
Anyone know how to do this?
[This post] didn't really help. Unless you tell me it's necessary to subclass UIGestureRecognizer
. Then, I think I could figure it out. Anyone have any examples?
Look at the UITapGestureRecognizer
documentation. There's two properties you want to look at:
numberOfTapsRequired
; andnumberOfTouchesRequired
Check out Three20's PhotoView. It does what you're looking to do.
Here's some code that should help:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[scrollView addGestureRecognizer:singleTap];
[singleTap release];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired: 2];
[scrollView addGestureRecognizer:doubleTap];
// this next line will hold callbacks to singleTap until enough time has gone by that doubleTap is not a possibility. If a doubleTap happens, singleTap will not get called. Otherwise, singleTap's callback gets called.
[singleTap requireGestureRecognizerToFail:doubleTap];
[doubleTap release];
精彩评论