2010-08-28 13 views

risposta

65

A partire da iOS 3.2, è possibile utilizzare la funzionalità di UIBezierPath s per creare un rettangolo arrotondato pronto all'uso (in cui solo gli angoli specificati sono arrotondati). È quindi possibile utilizzare questo come il percorso di un CAShapeLayer, e utilizzare questo come una maschera per il livello della vostra vista:

// Create the path (with only the top-left corner rounded) 
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
               byRoundingCorners:UIRectCornerTopLeft 
                cornerRadii:CGSizeMake(10.0, 10.0)]; 

// Create the shape layer and set its path 
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.frame = imageView.bounds; 
maskLayer.path = maskPath.CGPath; 

// Set the newly created shape layer as the mask for the image view's layer 
imageView.layer.mask = maskLayer; 

E questo è tutto - senza fare in giro manualmente definire forme Core Graphics, senza creazione mascherare le immagini in Photoshop . Il livello non ha nemmeno bisogno di invalidare. Applicare l'angolo arrotondato o passare a un nuovo angolo è semplice come definire un nuovo UIBezierPath e utilizzare il suo CGPath come percorso del livello maschera. Il parametro corners del metodo bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: è una maschera di bit, pertanto è possibile arrotondare più angoli eseguendo il raggruppamento.

NOTA - Le maschere di livello non vengono visualizzate se utilizzate in combinazione con il metodo renderInContext di CALayer. Se è necessario utilizzare questo, provare a arrotondare gli angoli con il seguente: Just two rounded corners?.

+1

Il codice mi ha davvero salvato. Se avessi la possibilità, avrei contrassegnato correttamente questa risposta. – itsaboutcode

+1

Una nota: se si modifica la dimensione della vista, sarà necessario ricreare la maschera. Pertanto, sottoclasse UIView e sovrascrive frame e bound. – Krumelur

+2

Potrei anche aggiungere un bordo attorno a questo percorso per evidenziare .. ?? –

2

ho fatto un metodo di risposta di StuDev:

+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner 
{ 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                byRoundingCorners:rectCorner 
                 cornerRadii:CGSizeMake(10.0, 10.0)]; 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = imageView.bounds; 
    maskLayer.path = maskPath.CGPath; 

    return maskLayer; 
} 

Esempio di utilizzo su un ImageView in un UITableViewCell:

if (indexPath.row == 0) 
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft]; 
else if (indexPath.row == self.arrayPeople.count - 1) 
    cell.imageView.layer.mask = [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft]; 

Grazie a StuDev per una grande soluzione!

+0

Potrei anche aggiungere un bordo attorno a questo percorso per evidenziare .. ?? –

0
+ (CAShapeLayer *) roundedCornerOnImage: (UIImageView *)imageView onCorner: (UIRectCorner)rectCorner 
{ 
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                byRoundingCorners:rectCorner 
                 cornerRadii:CGSizeMake(10.0, 10.0)]; 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = imageView.bounds; 
    maskLayer.path = maskPath.CGPath; 
    imageView.layer.mask=maskLayer 
    return maskLayer; 
} 


if (indexPath.row == 0) 
    [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerTopLeft]; 
else if (indexPath.row == self.arrayPeople.count - 1) 
    [Helper roundedCornerOnImage:cell.imageView onCorner:UIRectCornerBottomLeft]; 

Una risposta aggiornata a quanto sopra, non è necessario restituirlo e gestirlo. Può essere fatto in quella funzione.

0

Ho fatto una funzione per rendere il raggio d'angolo personalizzato dopo aver fatto qualche piccolo R & D come segue:

+(void)setConerRadiusForTopLeft:(BOOL)isForTopLeft ForTopRight:(BOOL)isForTopRight ForBottomLeft:(BOOL)isForBottomLeft ForBottomRight:(BOOL)isForBottomRight withCornerRadius:(float)cornerRadius forView:(UIView *)view 
{ 

    UIRectCorner corners = (isForTopLeft ? UIRectCornerTopLeft : 0) | 
          (isForTopRight ? UIRectCornerTopRight : 0) | 
          (isForBottomLeft ? UIRectCornerBottomLeft : 0) | 
          (isForBottomRight ? UIRectCornerBottomRight : 0); 

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds 
               byRoundingCorners:corners 
                cornerRadii:CGSizeMake(cornerRadius, cornerRadius)]; 

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 
    maskLayer.frame = view.bounds; 
    maskLayer.path = maskPath.CGPath; 
    view.layer.mask = maskLayer; 
} 
Problemi correlati