Handling touches inside UIWebview

前端 未结 10 1996
感情败类
感情败类 2020-11-30 22:46

I have created a subclass of UIWebView , and have implemented the touchesBegan, touchesMoved and touchesEnded methods.

相关标签:
10条回答
  • 2020-11-30 23:46

    You could put an UIView over your UIWebView, and overide the touchesDidBegin etc, then send them to your webview. Ex:

    User touches your UIView, which provokes a

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {  
        // Execute your code then send a touchesBegan to your webview like so:
    [webView touchesBegan:touches withEvent:event];
    return;
    }
    

    your UIView has to be over the webview.

    0 讨论(0)
  • 2020-11-30 23:47

    I would try overriding -sendEvent: on UIWindow, to see if you can intercept those touch events.

    0 讨论(0)
  • 2020-11-30 23:49

    Do you mean your sub-classed implementation is not called when touchesBegan, touchesMoved and touchesEnded are called?

    It sounds like a problem with how you've created an instance of the object. More details are required I think.

    (taken form comments)

    Header File

    #import <UIKit/UIKit.h> 
    
    @interface MyWebView : UIWebView { } @end 
    

    Implementation File

    #import "MyWebView.h" 
    
    @implementation MyWebView 
    
    - (id)initWithFrame:(CGRect)frame { 
        if (self = [super initWithFrame:frame]) { } return self; 
    } 
    
    - (void)drawRect:(CGRect)rect { 
        NSLog(@"MyWebView is loaded"); 
    } 
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
       NSLog(@"touches began"); 
    } 
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
        NSLog(@"Touches ended");     
    } 
    
    - (void)dealloc { 
       [super dealloc]; 
    } 
    
    @end
    
    0 讨论(0)
  • 2020-11-30 23:51

    No subclassing needed, just add a UITapGestureRecognizer :

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMethod)];
    [tap setNumberOfTapsRequired:1]; // Set your own number here
    [tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
    
    [self.myWebView addGestureRecognizer:tap];
    

    Add the <UIGestureRecognizerDelegate> protocol in the header file, and add this method:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    0 讨论(0)
提交回复
热议问题