menudo he visto a gente usando la capa de enorme rendimiento de vista de impacto para crear una esquina redondeada o dropshadow. Algo como esto:
[v.layer setCornerRadius:30.0f];
[v.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[v.layer setBorderWidth:1.5f];
[v.layer setShadowColor:[UIColor blackColor].CGColor];
[v.layer setShadowOpacity:0.8];
[v.layer setShadowRadius:3.0];
[v.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
.....
Esto tiene un impacto en el rendimiento enorme, especialmente con la sombra. Al poner vistas como esta en una UITableView (o cualquier cosa que se mueva) crearemos una experiencia de desplazamiento android-ish, eso no es deseable. Si necesita animar o mover la vista, evite crear esquinas redondeadas o dejar caer sombras como esta por cualquier medio.
Gráficos Meet Core
que he creado un simple subclase UIView que le muestre la forma de lograr el mismo resultado de una manera ligeramente diferente. Utiliza los gráficos centrales para dibujar la vista y, a diferencia del código anterior, no afecta el rendimiento.
Aquí está el código de dibujo:
- (void)drawRect:(CGRect)rect
{
CGContextRef ref = UIGraphicsGetCurrentContext();
/* We can only draw inside our view, so we need to inset the actual 'rounded content' */
CGRect contentRect = CGRectInset(rect, _shadowRadius, _shadowRadius);
/* Create the rounded path and fill it */
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:contentRect cornerRadius:_cornerRadius];
CGContextSetFillColorWithColor(ref, _fillColor.CGColor);
CGContextSetShadowWithColor(ref, CGSizeMake(0.0, 0.0), _shadowRadius, _shadowColor.CGColor);
[roundedPath fill];
/* Draw a subtle white line at the top of the view */
[roundedPath addClip];
CGContextSetStrokeColorWithColor(ref, [UIColor colorWithWhite:1.0 alpha:0.6].CGColor);
CGContextSetBlendMode(ref, kCGBlendModeOverlay);
CGContextMoveToPoint(ref, CGRectGetMinX(contentRect), CGRectGetMinY(contentRect)+0.5);
CGContextAddLineToPoint(ref, CGRectGetMaxX(contentRect), CGRectGetMinY(contentRect)+0.5);
CGContextStrokePath(ref);
}
Ver este blog: http://damir.me/rounded-uiview-with-shadow-the-right-way
impresionante! propiedad de shouldRasterize hizo el truco. ¡Gracias! – DZenBot
rasterización ¡Scame también me sorprendió por un tiempo! Usted se imaginaría que se basaría en la escala de la pantalla, pero no. : -/ – Dermot
Tenía un PNG parcialmente transparente con bordes dentados que tuve que animar (con una sombra, por supuesto). Fue 2/3 de la pantalla en tamaño, por lo que algunos píxeles. Esta solución todavía funcionó sin problemas. :) – toblerpwn