iOS: agrega UIGestureRecognizerDelegate a UIWebView

Quiero agregar UIGestureRecognizerDelegate a UIWebView, pero fallé.

si [self.view addsubView: webView]; Entonces, UIWebView está bien, pero no UIGestureRecognizerDelegate ..

si elimina [self.view addsubView: webView]; Entonces, UIGestureRecognizerDelegate está bien, pero no ve nada en absoluto.

- (void)viewDidLoad { [super viewDidLoad]; webView= [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, -2.0f, 320.0f, 415.0f)]; if (_refreshHeaderView == nil) { webScroller = (UIScrollView *)[[webView subviews] objectAtIndex:0]; [webScroller setDelegate:self]; EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - webScroller.bounds.size.height, self.view.frame.size.width, webScroller.bounds.size.height)]; view.delegate = self; [webScroller addSubview:view]; _refreshHeaderView = view; } [_refreshHeaderView refreshLastUpdatedDate]; [self.view addSubview:self.webView]; // Set up recognizers. UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; doubleTapRecognizer.numberOfTapsRequinetworking = 2; doubleTapRecognizer.delegate = self; [self.view addGestureRecognizer:doubleTapRecognizer]; UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; tapRecognizer.delegate = self; [self.view addGestureRecognizer:tapRecognizer]; } 

Si desea agregar Gesture Recognizer a webView y no a la vista principal de su aplicación que se encuentra debajo de webView, luego de agregar su webView como subView a la vista principal, debe cambiar su "addGestureRecognizer" de esta manera. .

  UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; doubleTapRecognizer.numberOfTapsRequinetworking = 2; doubleTapRecognizer.delegate =self; [self.webView addGestureRecognizer:doubleTapRecognizer]; UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; tapRecognizer.numberOfTapsRequinetworking = 1; tapRecognizer.delegate = self; [self.webView addGestureRecognizer:tapRecognizer]; 

Luego, como se trata de una UIWebView, también debe cumplir con el protocolo UIGestureRecognizerDelegate en su file de encabezado y luego devolver SÍ de los siguientes methods. De esta manera, los gestos de UIWebView y los tuyos serán manejados, de lo contrario los tuyos serán ocultados por los propios reconocedores de UIWebView.

 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return YES;} - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;} - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES;}