Ashi
Ashi

Reputation: 161

Get touch in webView

I have add UIWebView on UIViewControllerthen add one another UIViewController on same ViewController then i want get touch on UIwebView.

Upvotes: 0

Views: 109

Answers (4)

Owen Hartnett
Owen Hartnett

Reputation: 5935

Note that the touch from a gesture recognizer is to the view, not page-based. So if you want your app to respond to a particular item on your web page, no matter where it's scrolled or zoomed to, you'd be better off using URL schemes to send a signal to your app.

Upvotes: 0

Developer
Developer

Reputation: 760

You have to add Gesture

First set delegate in.h file

 -(void)ViewDidLoad
   {
          UITapGestureRecognizer   *gesture = [[UITapGestureRecognizer alloc]
       initWithTarget:self action:@selector(HandleGesture:)];
         gesture.delegate=self;
          gesture.numberOfTapsRequired = 1;
         [YourWebview addGestureRecognizer:gesture];
    }
   -(void)HandleGesture:(UITapGestureRecognizer *)sender
    {
        if (gesture.state == UIGestureRecognizerStateEnded)
        {

        }
        if (gesture.state == UIGestureRecognizerStateBegan)
        {
                    // You can write here any action
        }
    }
    - (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
      // test if our control subview is on-screen
        if ([touch.view isKindOfClass:[UIWebView class]]) {
             // You can handle any thing
            return YES;
         }return NO; // handle the touch
      }

Upvotes: 0

Balu
Balu

Reputation: 8460

For recognizing the touch events of UIWebview,UIScrollview you can use Gestures.don't forgot to add UIGestureRecognizerDelegate in .h file.

UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap)];
    single.numberOfTapsRequired = 1;
    single.delegate = self;
    [webview addGestureRecognizer:single];

-(void)oneTap{
    NSLog(@"single");
}

Upvotes: 0

Sumit Mundra
Sumit Mundra

Reputation: 3901

I have an UIWebView as a child of UIViewController's view. The solution that worked for me (for tab gesture) is this:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onDoubleTap:)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;

[self.webView addGestureRecognizer:tap];

My UIViewController is implementing the following method from the UIGestureRecognizerDelegate protocol (just retunning YES):

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Upvotes: 1

Related Questions