2011-01-24 15 views
5

ho un MapView, e voglio aggiungere un dropshadow, ma il metodo che ho provato non funziona:Aggiungere ombre per MKMapView

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

ottengo questo:

example

Sto facendo qualcosa di sbagliato?

risposta

9

risolti grazie a: http://blog.amarkulo.com/create-rounded-uiviews-with-shadow

utilizzando questo codice:

[[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

Un'altra alternativa (in realtà la soluzione proposta non ha funzionato per me, iOS SDK 4.3) è quello di racchiudere il MKMapView in un 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]; 

in questo modo è anche possibile animare la cornice del _mapContainer e ancora mantenere l'ombra nella sua posizione corretta.

Qui potete vedere i risultati effettivi here, se sei uno sviluppatore Apple.