iOS:将UIGestureRecognizerDelegate添加到UIWebView

时间:2012-06-28 06:01:21

标签: ios

我想将UIGestureRecognizerDelegate添加到UIWebView,但失败了。

如果[self.view addsubView:webView]; 所以UIWebView没问题,但不是UIGestureRecognizerDelegate ..

如果删除[self.view addsubView:webView]; 所以UIGestureRecognizerDelegate没问题,但什么都没看。

- (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.numberOfTapsRequired = 2;
doubleTapRecognizer.delegate = self;
[self.view addGestureRecognizer:doubleTapRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
tapRecognizer.delegate = self;
[self.view addGestureRecognizer:tapRecognizer];

}

1 个答案:

答案 0 :(得分:1)

如果您想将手势识别器添加到webView而不是webView下的应用程序主视图,将webView作为子视图添加到主视图后,则应该更改“addGestureRecognizer”此...

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

然后,因为这是一个UIWebView,您还应该符合头文件中的协议 UIGestureRecognizerDelegate ,然后从以下方法返回YES。这样UIWebView手势和你自己的手势都将被处理,否则你的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;}