2013-02-01 12 views
6

Buona giornata!Creazione di una vista angolo arrotondato

Sto cercando di rendere la mia vista (vista in vista principale) fare angolo arrotondato. Sto facendo così, ma non funziona. Qualche idea?

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
    currenView = [[UIView alloc] init]; 

    UIBezierPath *maskPath; 
    maskPath = [UIBezierPath bezierPathWithRoundedRect:currenView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(30.0, 30.0)]; 

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame = currenView.bounds; 
    maskLayer.path = maskPath.CGPath; 
    currenView.layer.mask = maskLayer; 


} 
return self; 
+1

È possibile effettuare semplicemente utilizzando la proprietà cornerRadius del livello. – Exploring

risposta

13

provare qualcosa di simile:

view.layer.cornerRadius = 5.0; 
view.layer.masksToBounds = YES; 

per ombra:

view.layer.shadowColor = [UIColor blackColor].CGColor; 
view.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); 
view.layer.masksToBounds = NO; 
view.layer.shadowRadius = 5.0f; 

Assicurarsi di importare <QuartzCore/QuartzCore.h>

+0

non funziona :(può essere qualcosa con l'uso di ARC? – Pavel

+0

Ho notato che non stai facendo nulla con l'UIView che stai allocando - currenView non sta andando da nessuna parte – Stavash

+0

Dopo questa riga aggiungi view.clipsToBounds = YES – CRDave

0

La tua vista non ha dimensione. la sua w e h è 0. Provare qualcosa di simile,

currentView = [[UIView alloc] initWithFrame:CGRectMake(0,0 200,200)]; 

e quindi applicare

currentView.layer.cornerRadius = 8.0; 
currentView.layer.masksToBounds = YES; 
currentView.layer.borderWidth = 1.0; 
+0

view.layer.cornerRadius = 5.0; view.layer.masksToBounds = YES; view.layer.shadowColor = [UIColor blackColor] .CGColor; view.layer.shadowOffset = CGSizeMake (1.0f, 1.0f); view.layer.masksToBounds = NO; view.layer.shadowRadius = 5.0f; se mi piace che gli angoli siano arrotondati ma non ci sia ombra. penso che il problema sia in view.layer.masksToBounds = NO; – Pavel

+0

per shadow segui la soluzione di stavash – karim

2

ecco il codice. Alloc avvia una visualizzazione e invia a questo metodo per ottenere gli angoli arrotondati. Puoi opzionalmente arrotondare qualsiasi angolo vuoi. Fornisci anche il colore del tratto dell'ombra.

-(void) setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners withColor: (UIColor*) color 
{ 
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(9.0, 9.0)]; 

CAShapeLayer* shape = [[[CAShapeLayer alloc] init] autorelease]; 
[shape setPath:rounded.CGPath]; 
shape.strokeColor = [[UIColor grayColor] CGColor]; 

view.backgroundColor=color; 
view.layer.mask = shape; 
} 

Chiama il metodo in questo modo.

[self setMaskTo:ABCView byRoundingCorners:UIRectCornerAllCorners withColor:[UIColor greenColor]]; 
Problemi correlati