2012-10-08 9 views
11

Ho un pulsante personalizzato che voglio che il bordo in alto a sinistra assomigli al normale rettangolo rotondo.Crea un angolo personalizzato con un rettangolo arrotondato in alto a sinistra?

ho trovato il codice che rende tutti gli angoli arrotondati:

_myButton.layer.cornerRadius = 8; 
_myButton.layer.borderWidth = 0.5; 
_myButton.layer.borderColor = [UIColor grayColor].CGColor; 
_myButton.clipsToBounds = YES; 

enter image description here

Come posso correggere il codice per renderlo turno solo nella parte in alto a sinistra?


Edit:

_myButton.layer.borderWidth = 2; 
_myButton.layer.borderColor = [UIColor blackColor].CGColor; 

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_myButton.bounds 
                byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight 
                 cornerRadii:CGSizeMake(7.0, 7.0)]; 

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 

maskLayer.frame = _myButton.bounds; 
maskLayer.path = maskPath.CGPath; 
_myButton.layer.mask = maskLayer; 
[maskLayer release]; 

Questo codice non funziona. L'intero pulsante scompare.

+0

Ecco una simile domanda/risposta su come è possibile utilizzare un UIBezierPath per dire che gli angoli per mantenere/maschera su un UIView: http://stackoverflow.com/questions/10995226/how-to -make-a-half-rounded-top-corner-rounded-texview-with-border – Brayden

+0

Ho usato il codice fornito lì. ma non funziona. Ho aggiunto il codice alla fine della domanda. puoi aiutarmi @Brayden? – Ali

risposta

23

Hai quasi capito, ma, dopo aver creato il tuo CAShapeLayer, usalo per aggiungerlo come sottolivello del livello del tuo pulsante, non come maschera alfa per nascondere alcune parti del tuo pulsante (in questo caso gli angoli).

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(10,300,300,40); 
[button setTitle:@"Hey" forState:(UIControlStateNormal)]; 
[button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)]; 

UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:button.bounds 
               byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight 
                 cornerRadii:CGSizeMake(7.0, 7.0)]; 

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.frame = button.bounds; 
shapeLayer.path = shapePath.CGPath; 
shapeLayer.fillColor = [UIColor clearColor].CGColor; 
shapeLayer.strokeColor = [UIColor blackColor].CGColor; 
shapeLayer.lineWidth = 2; 
[button.layer addSublayer:shapeLayer]; 

[self.view addSubview:button]; 
+0

grazie, guarda questa riga: '[_myButton.layer addSublayer: shapePath];' Vuoi dire 'shapeLayer'instead di shapePath? – Ali

+0

In realtà l'ho provato con shapeLayer ma non ha funzionato. – Ali

+1

questo è quello che intendevo sry. Cosa intendi per "non ha funzionato" come appariva? – AliSoftware

1
CAShapeLayer * positiveCorner = [CAShapeLayer layer]; 
positiveCorner.path = [UIBezierPath bezierPathWithRoundedRect: self.button.bounds 
              byRoundingCorners: UIRectCornerTopRight | UIRectCornerBottomRight 
                cornerRadii: (CGSize){5, 5}].CGPath; 

self.button.layer.mask = positiveCorner; 
Problemi correlati