zucknet
zucknet

Reputation: 41

iOS:add UIGestureRecognizerDelegate to UIWebView

I want add UIGestureRecognizerDelegate to UIWebView,but failed.

if [self.view addsubView:webView]; So UIWebView is ok,but not UIGestureRecognizerDelegate..

if remove [self.view addsubView:webView]; So UIGestureRecognizerDelegate is ok,but view nothing at all.

- (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];

}

Upvotes: 3

Views: 1354

Answers (1)

Kevin Horgan
Kevin Horgan

Reputation: 1580

If you want to add the Gesture Recognizer to the webView and not the main view of your application which is under the webView, after you add your webView as a subView to the main View, then you should change your "addGestureRecognizer" like this...

    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];

Then as this is a UIWebView you should also conform to the protocol UIGestureRecognizerDelegate in your header file and then return YES from the following methods. This way the UIWebView gestures and your own will both be handled, otherwise yours will be hidden by the UIWebView's own recognizers.

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

Upvotes: 1

Related Questions