I am trying to detect double tap in a uiwebview. following is the code. when i debug, debugger comes to initwithcoder but never to touchesbegan nor to selector function. why so ?
I am putting a UIWebView in my xib file and setting identity inspector class to MyWebView
#import "MyWebView.h"
@implementation MyWebView
- (void) initTap {
UITapGestureRecognizer *singleFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerDoubleTap:)];
singleFingerDoubleTap.numberOfTouchesRequired = 1;
singleFingerDoubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:singleFingerDoubleTap];
[singleFingerDoubleTap release];
}
- (id)initWithCoder:(NSCoder*)coder
{
if (self = [super initWithCoder:coder]) {
// Initialization code.
[self initTap];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
[self initTap];
}
return self;
}
-(void)handleSingleFingerDoubleTap:(id)sender {
NSLo开发者_运维问答g(@"Doulble click detected !");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (touch.tapCount == 2) {
//put you zooming action here
NSLog(@"Doulble click detected !");
}
}
- (void)dealloc {
[super dealloc];
}
@end
You just need to add delegate to the recognizer
singleFingerDoubleTap.delegate = self;
Then implement the following method to return YES.
#pragma mark Gesture recognizer delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Don't forget to add conform declaration to your class file.
Like this
@interface ORWebViewController : UIViewController<UIGestureRecognizerDelegate>
In case, you want to override the default zooming action on double tap, this will guide you.
UPDATE:
Please do not consider above mentioned reference as the author has moved it.
You can find the references from Disabling default zoom effect.
精彩评论