2010-08-28 16 views

Respuesta

65

A partir de iOS 3.2, puede usar la funcionalidad de UIBezierPath s para crear un rect redondeado listo para usar (donde solo se redondean las esquinas que especifique). A continuación, puede utilizar esto como el camino de un CAShapeLayer, y utilizar esto como una máscara para la capa de su punto de 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; 

y eso es todo - sin jugar un poco manualmente la definición de formas de núcleo de gráficos, sin creación de enmascarar imágenes en Photoshop . La capa ni siquiera necesita invalidación. Aplicar la esquina redondeada o cambiar a una nueva esquina es tan simple como definir una nueva UIBezierPath y usar su CGPath como la ruta de la capa de la máscara. El parámetro corners del método bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: es una máscara de bits, por lo que las esquinas múltiples se pueden redondear haciendo OR juntos.

NOTA - Las máscaras de capa no se procesarán cuando se utilicen en combinación con el método de renderizado de CALayer. Si necesita usar esta prueba, trate de redondear las esquinas con lo siguiente: Just two rounded corners?.

+1

Su código realmente me ha salvado. Si tengo la oportunidad, habría marcado esta respuesta correcta. – itsaboutcode

+1

Una nota: si cambia el tamaño de su vista, tendrá que volver a crear la máscara. Por lo tanto, la subclase UIView y anula el marco y los límites. – Krumelur

+2

¿Podría también agregar un borde alrededor de esta ruta para resaltar ... ?? –

2

Hice un método de respuesta de 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; 
} 

Ejemplo de uso en un imageview en 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]; 

Gracias a StuDev para una gran solución!

+0

¿Podría también agregar un borde alrededor de esta ruta para resaltar ... ?? –

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 respuesta actualizada a la anterior, no necesita devolverla ni administrarla. Se puede hacer en esa función.

0

he hecho una función para hacer que el radio de la esquina personalizada después de hacer algunas pequeñas R & D de la siguiente manera:

+(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; 
} 
Cuestiones relacionadas