繁体   English   中英

如何将MKMapView(或任何UIView)裁剪为自定义形状?

[英]How do I crop a MKMapView (or any UIView) into a custom shape?

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

顶部为弧形的地图视图

元素应该能够出现在顶部的圆弧后面,因此该视图似乎不是正方形的。 我知道使用CALayer应该可以,但是也许有人以前做过?

如果将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 Maps不同。

;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM