【问题标题】:Passing a PanGesture to a subview将 PanGesture 传递给子视图
【发布时间】:2014-09-16 07:57:52
【问题描述】:

我正在构建一个 iOS 应用程序。我有一个 UIWebView 作为子视图添加到 self.view,然后是另一个视图,即 mapView,作为 Web 视图的子视图添加。但是 mapView 被发送到 webView 的后面。 webView 的背景是透明的,因此可以看到地图。 看代码:

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

我想做的是将 webView 的手势传递给 mapView,以便用户可以拖动地图。

我已将 webView 和 mapView 的 cancelsTouchesInView 属性标记为 NO。

我为 webView 添加了一个手势识别器。选择器确实被调用。但是接下来我该怎么做呢?

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

  [self.webView addGestureRecognizer: self.webPanGesture];

我在 webView 选择器中调用了 gestureRecognizerShouldBegin 方法,但它不起作用。

- ( 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
}

我也调用这个函数

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

但它不会使地图可拖动,它只是移动它。

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

如何将操作定位到 mapView 以便在 webView 上拖动时拖动地图?

【问题讨论】:

  • 但是你这样做有什么意义呢?为什么地图在webview后面?你想达到什么目的?
  • 问题是有html元素必须在地图上方,所以我把地图放在底部。
  • 好的,但最后你可以分别显示,而不是一个在另一个后面。
  • 顺便说一句,我还尝试使用 CALayer 掩码来显示按钮。但这更难。
  • 如果您需要地图上的按钮,然后以编程方式创建一个按钮并将其作为子视图添加到 mapView。

标签: ios objective-c uigesturerecognizer subview hittest


【解决方案1】:

我确定您应该在 mapView 上使用叠加层 (MKOverlay) 在地图上显示内容。因为这是实现您所需要的更简单的方法。

请阅读此Adding Annotations to a Map

【讨论】:

  • 这与覆盖无关。他无法应用/传递在 webView 上应用的平移手势。因此,当 mapView 无法获得控制时,覆盖就没有好处了。
  • 我关于一般的解决方案。
【解决方案2】:

我在这里找到了解决方法,所以请查看此link 可能会有所帮助。

简而言之,webView 不处理 touchBegan 方法,因此您需要对其进行子类化,并且在 touch started 方法中您可以传递以下内容,

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

  [_mapView touchesBegan:touches withEvent:event];
}

或查看以下方法,

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

hitTest 上面是指这个link

希望,这么多信息对你有用。

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2017-06-16
    相关资源
    最近更新 更多