3

我想将 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];

}
4

1 回答 1

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;}
于 2012-06-28T09:51:53.867 回答