简体   繁体   English

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

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

I'm trying to size an MKMapView in the following way: 我正在尝试通过以下方式调整MKMapView的大小:

顶部为弧形的地图视图

Elements should be able to appear behind the arc at the top, so it appears that the view is not square. 元素应该能够出现在顶部的圆弧后面,因此该视图似乎不是正方形的。 I know this should be possible with CALayer, but perhaps someone has done it before? 我知道使用CALayer应该可以,但是也许有人以前做过?

This should be quite possible if you put your MKMapView within a UIView, then apply a mask to that. 如果将MKMapView放在UIView中,然后对其应用蒙版,则应该很有可能。

Here's a very (!) basic example: 这是一个非常(!)基本示例:

在此处输入图片说明

All I've done is put my map in a UIView called "mapContainer" and added a mask to it using this: 我所做的只是将地图放入名为“ 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;
}

And you could add a border to it... 您可以为其添加边框...

在此处输入图片说明

..with a few more lines... ..还有几行...

-(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;
}

I hope this points you in the right direction. 我希望这可以为您指明正确的方向。

Unlike Apple Maps. 与Apple Maps不同。

;-) ;-)

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

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