iOS如何管理地图上复杂的视图

新项目有些类似于滴滴,地图上面有许多复杂的控件,如图所示:这是一种状态下的页面,在另一种状态下除了地图其余控件都隐藏,显示其他的控件。

最初的设想是用view或者child controller来管理不同状态的控件,但是由于view覆盖在地图上,就无法操作地图。

图片描述

阅读 2.3k
2 个回答

解决方案:重写UIView的 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 的方法

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
     self.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150.f, 50.f)];
        [self addSubview:button];
        [button setTitle:@"button" forState:UIControlStateNormal];
        [button addTarget: self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button.backgroundColor = [UIColor orangeColor];
        button.center = self.center;
    }
    return self;
}

- (void)buttonAction {
    NSLog(@"buttonAction");
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView * view in [self subviews]) {
        if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
            return YES;
        }
    }
    return NO;
}

@end

"但是由于view覆盖在地图上,就无法操作地图"

我试了一下,地图上覆盖一个 View 是不会吸收地图上的手势的,你是不是把覆盖上去的 view.userInteractionEnabled = NO;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题