简体   繁体   English

如何在不禁用clipsToBounds的情况下向MKMapView添加阴影

[英]How to add a drop shadow to an MKMapView without disabling clipsToBounds

I'm having an issue when I attempt to add a drop shadow to an MKMapView 's layer, in order for the shadow to be visible I have to set the view's clipsToBounds property to false. 尝试向MKMapView的图层添加阴影时出现问题,为了使阴影可见,我必须将视图的clipsToBounds属性设置为false。 However, doing so causes the map tiles to draw outside the view's boundaries, overlapping the shadow and cutting of parts of my view. 但是,这样做会导致地图图块在视图边界之外绘制,与阴影重叠并切割了部分视图。 The result looks something like this: 结果看起来像这样:

iPhone 5.1 Simulator中的错误截图

I'm wondering if there's a way to draw a drop shadow without disabling bounds clipping or otherwise get the drop shadow to appear without this ugly visual bug. 我想知道是否有一种方法可以在不禁用边界修剪的情况下绘制阴影,或者在没有此难看的视觉错误的情况下使阴影显示出来。 My code for setting the drop shadow looks like this: 我设置投影的代码如下所示:

self.mapView.layer.borderWidth = 5.0;
self.mapView.layer.borderColor = [[UIColor whiteColor] CGColor];
self.mapView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
self.mapView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.mapView.layer.shadowRadius = 5.0;
self.mapView.layer.shadowOpacity = 0.2;
self.mapView.clipsToBounds = NO;

Since the layer is owned by the MKMapView , it's generally not a great idea to be touching it yourself. 由于该图层归MKMapView拥有,因此自行触摸通常不是一个好主意。 (This is the kind of thing that's likely to break in weird ways in later OS versions, and behave in unpredictable ways (it'd be interesting to see if that even works at all with the new iOS 6 3D maps). With layer-backed views on OS X, you're not supposed to touch the layer directly at all (unless it's layer-hosting view, but that's a different discussion)) (这种情况很可能在以后的OS版本中以怪异的方式破坏,并以无法预测的方式表现(很有趣的是,它甚至对于新的iOS 6 3D地图是否也适用)。支持OS X上的视图,您根本不应该直接接触该层(除非它是层托管视图,但这是不同的讨论)。

To get a shadow underneath, just make your own new CALayer with a shadow positioned underneath the map. 要在下方CALayer阴影,只需制作自己的新CALayer并在地图下方CALayer阴影即可。 Alternatively nest the MKMapView as a subview of your own UIView , and add the shadow to your view (that has no need for clipping) instead. 或者,将MKMapView嵌套为您自己的UIView的子视图,然后将阴影添加到您的视图中(不需要裁剪)。

You have to create two views, one for the shadow and an other for the rounded corners. 您必须创建两个视图,一个用于阴影,另一个用于圆角。

More info here : UIView Round Corners with Shadow 更多信息在这里: UIView带阴影的圆角

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

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