【问题标题】:How do I crop a MKMapView (or any UIView) into a custom shape?如何将 MKMapView(或任何 UIView)裁剪为自定义形状?
【发布时间】:2025-11-29 06:05:01
【问题描述】:

我正在尝试通过以下方式调整 MKMapView 的大小:

元素应该能够出现在顶部圆弧的后面,所以看起来视图不是方形的。我知道 CALayer 应该可以做到这一点,但也许有人以前做过?

【问题讨论】:

    标签: ios uiview calayer quartz-graphics


    【解决方案1】:

    如果您将 MKMapView 放在 UIView 中,然后对其应用掩码,这应该是很有可能的。

    这是一个非常(!)的基本示例:

    我所做的只是将我的地图放在一个名为“mapContainer”的 UIView 中,并使用它为其添加了一个遮罩:

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        CGRect rect = CGRectInset(self.view.frame, 70, 70);
        UIView* newView = [[UIView alloc] initWithFrame:rect];
        newView.layer.cornerRadius = 55.0;
        newView.backgroundColor = [UIColor redColor];
    
        self.mapContainer.layer.mask = newView.layer;
    }
    

    你可以给它加个边框...

    ..还有几行...

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        CGRect rect = CGRectInset(self.view.frame, 70, 70);
        UIView* newView = [[UIView alloc] initWithFrame:rect];
        newView.layer.cornerRadius = 55.0;
        newView.backgroundColor = [UIColor redColor];
    
        UIView* borderView = [[UIView alloc] initWithFrame:rect];
        borderView.backgroundColor = [UIColor clearColor];
        borderView.layer.cornerRadius = 55.0;
        borderView.layer.borderWidth = 3.0;
        borderView.layer.borderColor = [[UIColor redColor] CGColor];
        [self.mapContainer addSubview:borderView];
        [self.mapContainer bringSubviewToFront:borderView];
    
        self.mapContainer.layer.mask = newView.layer;
    }
    

    我希望这会为您指明正确的方向。

    与 Apple 地图不同。

    ;-)

    【讨论】:

      最近更新 更多