OGeek|极客世界-中国程序员成长平台

标题: ios - 可以将触摸事件转发到 MKMapView 吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:03
标题: ios - 可以将触摸事件转发到 MKMapView 吗?

我想将 map View 显示为永久背景,而其他 View 显示在其顶部(我将顶部 View 的 alpha 设置为 0.9 之类的值,以便 map 在下方隐约可见)和在某些时候 map 会显示出来。

我有一个容器 View ,它位于 map View 的顶部,我想知道是否可以将容器 View 边界内发生的触摸事件传递给 map View ,以便可以滚动等。这是一个显示架构示例的草图项目。 enter image description here

enter image description here

(容器 View 位于 map View 的下半部分之上,容器 View 和包含的 View Controller 的 View 的 alpha 均为 0,因此对用户而言, map 在整个屏幕上都是可见的)。

将在包含 View Controller 的 View 或 subview Controller 中发生的触摸事件转发到 map 背景 View Controller 很容易。

如果我像这样将触摸事件传递给 map View

- (void) touchesBeganNSSet *)touches withEventUIEvent *)event
{
    MapBackgroundViewController *parent = (MapBackgroundViewController *) self.parentViewController;
    [parent.mapView touchesBegan:touches withEvent:event];
}

然后什么都没有发生。

有没有办法将触摸事件传递给 map View 以使其滚动等?



Best Answer-推荐答案


是的,你可以这样做。

我所做的是子类 UIView 并覆盖 hitTest:withEvent: 以便通过触摸,除非触摸 subview 。像这样的:

@implementation PassthroughView

- (UIView *)hitTestCGPoint)point withEventUIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    return view == self ? nil : view;
}

@end

然后我将这个类分配给我的容器 View 和包含的 View Controller 在 IB 中的主视图。因此,您仍然可以与包含的 View Controller 的内容进行交互,但对容器本身的触摸会传递到 map 。

关于ios - 可以将触摸事件转发到 MKMapView 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22736308/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4