消息关闭
    暂无新消息!

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

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


2个回答

︿ 0

解决方案:重写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

︿ 0

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

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