UIWebView无法识别TapGesture(根本不会调用方法)

时间:2013-02-25 12:49:27

标签: iphone ios6 uiwebview uigesturerecognizer

当我按照我在StackOverflow上找到的答案时,我需要让UIWebView认出touch正在实施以下内容:

WebVC.h

#import <UIKit/UIKit.h>

@interface WebVC : UIViewController <UIWebViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, copy) NSString *test;

@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@property (strong, nonatomic) UITapGestureRecognizer *tapGestureR;

- (void)handleTap:(id)sender;

@end

WebVC.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.delegate = self;

    self.tapGestureR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    self.tapGestureR.delegate = self;
    [self.webView addGestureRecognizer:self.tapGestureR];
}

- (void)handleTap:(id)sender
{
     NSLog(@"tapDetected");
}

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

你知道我为什么看不到NSLog吗? <{1}}根本不会被调用。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

self. webView.userInteractionEnabled = YES;

使用userInteractionEnabled方法并设置为YES。我认为这会对你有所帮助。

<强>编辑:

在WebVC.m类中使用-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer方法。

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
     NSLog(@"shouldRecognizeSimultaneouslyWithGestureRecognizer");
    return YES;
}

答案 1 :(得分:1)

    for(UIGestureRecognizer *gesture in [webview gestureRecognizers])){
       if([gesture isKindOfClass:[UITapGestureRecognizer class]){
         if (gesture.numberOfTapsRequired == 1) 
            [webview removeGestureRecognizer:gesture];
       }
    }

然后添加你的手势。 就像你以前做过的那样

相关问题