开发者

how to recognise Single tap using UIWebView

开发者 https://www.devze.com 2023-01-08 19:52 出处:网络
I am using UIWebView to show a pdf file in my MainViewController. On single tap I want to load a new View to the MainViewController.

I am using UIWebView to show a pdf file in my MainViewController. On single tap I want to load a new View to the MainViewController.

But the UIWebView is not allowing default UITouch event

-(void) touchesBegan:(NSSet *)touches withEven开发者_运维百科t:(UIEvent *)event
{

}

How to solve this problem?

Regards


UIWebView a is subclass of UIScrollView. You can use touchesShouldBegin:withEvent:inContentView: method defined in UIScrollView documentation.

You need to define this method in your UIWebView own class:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
// DO SOMETHING
return NO;}


@Pierre

Thanks

I did the following :

@implementation UIViewTouch

. . .

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view 
{
    // DO SOMETHING
    return NO;
}

In the view controller implementation:

-(void)viewDidLoad
{

    CGRect frame = CGRectMake(0, 0, 500, 1000);     
    UIViewTouch *viewTouch = [[UIViewTouch alloc]initWithFrame:frame];

    NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"page1" ofType:@"pdf"];  
    NSURL *url = [NSURL fileURLWithPath:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [viewTouch loadRequest:requestObj]; [self.view addSubview:viewTouch];

}


Create a UIView sub class containing the whole UIWebView. Then the event hitTest will be fired on touching the webview. Note: Don't put anything to the container except the webView.

@interface myWebViewContainer : UIView
... ...
@end

Then override the hitTest event:

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"Hit test %@", [event description]);
    UIView * returnView = [super hitTest:point withEvent:event];
    [delegate userDidTapWebView];// notify the delegate that UIWebView is being touched by some naughty user :P


    return  returnView;
}


Ideally webView userInteractionEnabled should be set to NO and on top of the web view that is the view controller. We can use a TapGesture which will load another view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号