给MKMapView添加阴影

5

我有一个MapView,想要为它添加一个阴影效果,但是我尝试的方法没有生效:

- (void)viewDidLoad {
    [super viewDidLoad];

    mapView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
    mapView.layer.shadowOffset = CGSizeMake(10.0f, 10.0f);
    mapView.layer.shadowOpacity = 1.0f;
    mapView.layer.shadowRadius = 10.0f;
}

我收到了这个:
我做错什么了吗?
2个回答

10

感谢解决方案:http://blog.amarkulo.com/create-rounded-uiviews-with-shadow

使用以下代码:

[[mapView layer] setMasksToBounds:NO];
[[mapView layer] setShadowColor:[UIColor blackColor].CGColor];
[[mapView layer] setShadowOpacity:1.0f];
[[mapView layer] setShadowRadius:6.0f];
[[mapView layer] setShadowOffset:CGSizeMake(0, 3)];

2
另一种选择(实际上,对我来说提出的解决方案在iOS SDK 4.3中不起作用)是将MKMapView封装在UIView中:
_mapContainer = [[UIView alloc] initWithFrame: CGRectMake (0.0f, 44.0f, 320.0f, container.frame.size.height - 44.0f)];
_mapContainer.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_mapContainer.layer.masksToBounds = NO;
_mapContainer.layer.shadowColor = [UIColor blackColor].CGColor;
_mapContainer.layer.shadowOffset = CGSizeMake (0.0f, 10.0f);
_mapContainer.layer.shadowOpacity = 0.6f;
_mapContainer.layer.shadowRadius = 5.0f;
[container addSubview: _mapContainer];
[_mapContainer release];

_mapView = [[MKMapView alloc] initWithFrame: _mapContainer.bounds];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[_mapContainer addSubview: _mapView];
[_mapView release];

这样,您也可以对 _mapContainer 的帧进行动画处理,同时仍然保持阴影在正确的位置。
如果您是注册的苹果开发者,可以在这里查看实际结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接