para descargar apple iphone objective-c ios core-animation shadow

iphone - descargar - La forma más rápida de hacer sombras en iOS?



ipad (2)

A menudo he visto a gente usando la capa de la vista de impacto ENORME de rendimiento para crear una esquina redondeada o sombra de gotas. 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 GRAN impacto en el rendimiento, 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.

Conoce los gráficos básicos
Creé una subclase UIView simple para mostrarle cómo 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); }

Vea este blog: http://damir.me/rounded-uiview-with-shadow-the-right-way

QuartzCore .layer.shadow''s de alto rendimiento. Parecen necesitar ser re-renderizados cada vez que algo cambia, causando que todo se retrase.

Gradiente de Coregraphics (para sombras de 1 vía): no se ve bien. si tu gradiente va de 0.3 alfa a 0, tiene un efecto extraño donde puedes ''verlo''. Simplemente no se ve bien, o natural. Tal vez no esté titubeante, pero estoy seguro de que escuché los gradientes gráficos centrales. Es extraño, no sé.

Sombra de Coregraphics: tómate un tiempo para renderizar a medida que los configuras, pero de lo contrario un gran rendimiento. Es solo ese segundo que estás esperando que aparezca una vista porque primero tiene que mostrar su sombra, ese es el problema.

Así que debo estar perdiendo algo. ¿Hay otro método que se ve bien, y es rápido tanto en tiempo de renderización como en rendimiento?


Agregar un shadowPath debería darle un gran impulso al rendimiento. El siguiente ejemplo asume que solo quieres la sombra en los lados de tu vista

CGPathRef path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath; [view.layer setShadowPath:path];

EDITAR: de forma predeterminada, un CALayer dibuja una sombra durante las animaciones, el siguiente código le permite almacenar en caché la sombra como un mapa de bits y reutilizarla en lugar de volver a dibujarla:

self.view.layer.shouldRasterize = YES; // Don''t forget the rasterization scale // I spent days trying to figure out why retina display assets weren''t working as expected self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;