0

I am building a iOS app. I have a UIWebView that is added as a subview to self.view, then another view, which is mapView, added as a subview of the web view. But the mapView is send to the back of the webView. The background of the webView is transparent so that one can see the map. see the code:

[self.webView addSubview: self.mapView];
[self.webView sendSubviewToBack: self.mapView];

Well what I am trying to do is to pass the gestures of the webView to the mapView so that the user can drag the map.

I have marked the cancelsTouchesInView property to NO for both the webView and the mapView.

I have added a gesture recognizer for the webView. The selector does get called. But what am I supposed to do next?

    self.webPanGesture = [[UIPanGestureRecognizer  alloc] initWithTarget:self action: @selector(handleWebPanGesture:)];

  [self.webView addGestureRecognizer: self.webPanGesture];

I called the gestureRecognizerShouldBegin method in the webView selector, but it doesn't work.

- ( void ) handleWebPanGesture: ( UIPanGestureRecognizer  *)gesture
{
  NSLog(@"WebPanGesture recognizer called!");
  [self.mapView gestureRecognizerShouldBegin: gesture];
  [self panAction: gesture];
  self.mapPanGesture = gesture; // the mapPanGesture property is the Gesture recognizer for the map
}

I also call this function

- ( IBAction )panAction:(UIPanGestureRecognizer *)sender {
  NSLog(@"panAction called!");
  CGPoint move = [sender translationInView:self.webView];
  CGPoint newCenter = subViewCenter;
  newCenter.x += move.x; newCenter.y += move.y;
  self.myMapView.mapView.center = newCenter;
}

but it doesn't make the map draggable, it just moves it.

self.mapPanGesture = gesture //doesn't work as well.

How can I target the actions to the mapView so that the map gets dragged when drag on the webView?

nikozavar
  • 21
  • 4
  • But whats your point of doing this? Why is map behind the webview? What are you trying to achieve? – nikhil84 Sep 16 '14 at 08:29
  • well the problem is that there are html elements that have to be above the map, so that's why I send the map at the bottom. – nikozavar Sep 16 '14 at 08:31
  • 1
    Ok fine but at end you could show both separately rather than one behind other. – nikhil84 Sep 16 '14 at 08:36
  • Btw I also tried using CALayer masks in order to display the buttons. But it was even harder. – nikozavar Sep 16 '14 at 08:36
  • 1
    if u need button on map then create a button programmatically and add it as subview to mapView. – nikhil84 Sep 16 '14 at 08:45
  • Well I might try it later on, but isn't the result achievable? – nikozavar Sep 16 '14 at 08:54
  • so correct me if I'm understanding it wrong.. that when a user perform pan gesture in webView then u want that same pan gesture to be applied on mapView(but mapView has it's own pan gesture by default) – nikhil84 Sep 16 '14 at 11:27

2 Answers2

0

I sure you should use overlays (MKOverlay) on mapView to show content on map. Because this is much easier way to achieve what you need.

Please read this Adding Annotations to a Map

sage444
  • 5,661
  • 4
  • 33
  • 60
0

Here I found a way around so do check out this link might be helpful.

In short, webView doesn't handle touchBegan method so u need to subclass that and in touch began method u could pass the following,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
   NSLog(@"%@",event);
  [super touchesBegan:touches withEvent:event];

  [_mapView touchesBegan:touches withEvent:event];
}

or check out for below method,

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
   UIView *hitView = [super hitTest:point withEvent:event];

  // If the hitView is THIS view, return the view that you want to receive the touch instead:
   if (hitView == self) {
    return otherView;
  }
  // Else return the hitView (as it could be one of this view's buttons):
  return hitView;
}

Above hitTest is with reference to this link.

Hope, this much info is useful to u.

Community
  • 1
  • 1
nikhil84
  • 3,235
  • 4
  • 22
  • 43