将阴影添加到MKMapView

时间:2011-01-24 17:10:00

标签: iphone ios mkmapview shadow

我有一个MapView,我想添加一个Drophadow,但我尝试的方法不起作用:

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

我明白了:

example

我做错了吗?

2 个答案:

答案 0 :(得分: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)];

答案 1 :(得分: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的框架设置动画,并将阴影保持在正确的位置。

如果您是注册的Apple开发人员,可以在此处查看实际结果here