¡Hola! Quiero dibujar una esquina redondeada de mi UIView
. Solo uno, otros no se pueden cambiar.Cómo dibujar una esquina redondeada de mi UIView.
Respuesta
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?.
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!
¿Podría también agregar un borde alrededor de esta ruta para resaltar ... ?? –
+ (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.
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;
}
- 1. Java error de esquina redondeada
- 2. Cómo dibujar una ruta suave/redondeada?
- 3. CSS3: Transparente esquina redondeada Problema
- 4. ¿Cómo puedo hacer una cuadrícula de esquina redondeada?
- 5. ¿Cómo crear un NSImageView con una esquina redondeada?
- 6. cómo configurar la esquina redondeada para una UITextView?
- 7. svg css esquina redondeada no funciona
- 8. ¿Cómo creo un contenedor de esquina redondeada de WPF?
- 9. ¿Cree una esquina de botón de rectángulo redondeada personalizada en la esquina superior izquierda?
- 10. Mesa personalizada de iPhone Mesa de esquina redondeada
- 11. Java - panel de esquina redondeada con la composición en paintComponent
- 12. UIView con esquina redonda y borde blanco
- 13. Cómo usar la etiqueta de esquina redondeada en iphone, UILabel Round Corners
- 14. ¿Volver a dibujar un UIView con una animación de desvanecimiento?
- 15. Obtener un contorno de área de contenido de esquina redondeada de CALayer como trazado de recorte
- 16. ¿La mejor manera de eliminar el halo de esquina redondeada de CSS?
- 17. UIView: esquinas redondeadas sin problemas de rendimiento
- 18. CSS3 Esquina redondeada para el elemento de entrada sin js/images
- 19. Estilo lista desordenada para mostrar los elementos de la lista en la caja esquina redondeada
- 20. Cómo dibujar un rectángulo redondeado en el lienzo HTML?
- 21. dibujar fuera de los límites de UIView desde drawRect
- 22. ¿Cómo dibujar una línea entre dos divs?
- 23. Radio de la esquina para una esquina particular
- 24. WPF Design: Cómo hacer el control del usuario con la esquina redondeada y el efecto de sombra
- 25. ¿Cómo animo/giro un UIView 90 degress desde su esquina superior derecha?
- 26. ¿Cómo puedo dibujar un rectángulo redondeado Etiqueta o Vista?
- 27. ¿Cómo puedo dibujar el diseño de mi base de datos?
- 28. ¿Cómo evito que una imagen desborde un recuadro de esquina redondeado con CSS3?
- 29. Codificación de mí mismo en una esquina
- 30. ¿Cómo usar una forma dibujable junto con una imagen?
Su código realmente me ha salvado. Si tengo la oportunidad, habría marcado esta respuesta correcta. – itsaboutcode
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
¿Podría también agregar un borde alrededor de esta ruta para resaltar ... ?? –