Comparing the CAShapeLayer and the DrawRect

Reprinted from: iOS learning: CAShapeLayer and DrawRect contrast, painting and graphics UIBezierPath

Use CAShapeLayersome of the following advantages

  • Rendering fast. CAShapeLayerUse hardware acceleration, draw the same graphic than with the Core Graphicsmuch faster.

  • Efficient use of memory. A CAShapeLayerdo not like the ordinary CALayerto create a graphic boarding the same, so no matter how big, will not take up too much memory.

  • It will not be clipped boundary layer. One CAShapeLayercan draw outside the boundaries. Your path will not be like the layers using Core Graphicsordinary CALayeras to be cut off.

  • Pixelation does not occur. When you give CAShapeLayerdo 3D transformations, it is not a boarding diagram of a normal layer becomes pixelated same.

Comparing the CAShapeLayer and the DrawRect

  • CAShapeLayerInherited from CALayer, and therefore can be used CALayerfor all properties. However, CAShapeLayerthe needs and Bezier curves with the use of makes sense.

  • -(void)drawRect:(CGRect)rect{}Just a way it is a UIViewway to override this method to complete our draw graphics capabilities.

  • DrawRect: DrawRect belong CoreGraphic framework, taking up CPU, consumption performance.

  • CAShapeLayer: CAShapeLayer belong CoreAnimation frame by the GPU to render graphics, saving performance. Animation rendering submitted directly to the phone GPU, does not consume within.

CAShapeLayer relationship with the UIBezierPath

  • CAShapeLayer in shape represents the shape of meaning, so it is necessary shape to take effect.

  • Bezier curves can be created based on a path vector, and UIBezierPaththe class is CGPathRefencapsulated.

  • Bezier curves to CAShapeLayerprovide a path CAShapeLayerrendered in the path provided. Path will be closed, so map outShape。

  • Bezier curve is used as CAShapeLayer path, which path is a closed curve connected end to end, even if the Bezier curve is not a closed loop.

CAShapeLayer与CALayer

  • When it is initialized CALayer common is the need for the value of a frame, the frame is generally consistent with the assigned value bounds value for a given view, which itself is shaped, and rectangular.

  • CAShapeLayer frame will need to specify initialization value, but it does not have a shape, its shape from its property path.

  • Unlike the CALayer CAShapeLayer have attributes, attributes inherited from its CALayer does not work when drawn.

CAShapeLayer has a few very important

  • CAShapeLayer attached to a given path, it must be given path, and, even if not complete path automatically connected end to end.

  • CAShapeLayer property: strokeStart and strokeEnd represents the percentage in this path in the occupied.

  • CAShapeLayer animation limited only along the edge of the animation effect, it can not achieve the effect of filling.

CAShapeLayer object properties list

Property name description
path CGPathRef objects, graphics sideline path
lineWidth The width of the sideline
strokeColor Sideline colors
lineDashPattern Set the edge style, the default is a solid line, the array is an array NSNumber, array values ​​in the broken line sequentially represents the length of a single line, and the length of the blank, such as: @ array [2,2,3,4] represents 2 is a line having a length, the length of the blank 2, a length of line 3, dotted lines the length of the blank 4 continues circulating.
lineDashPhase Starting position of the pattern edges, i.e., if the set lineDashPattern @ [2,2,3,4], lineDashPhase is the length of a first line of a start position of 2
lineCap Line end style, the default kCALineCapButt
lineJoin Style line at the inflection point, the default kCALineJoinMiter
strokeStart strokeEnd CGFloat type, [0,1] Videos showing the start and end edge (i.e., percentage on the route)
fillColor CGColorRef objects, graphics fill color, default is black

Reproduced in: https: //www.jianshu.com/p/55864e5f8a04

Guess you like

Origin blog.csdn.net/weixin_34351321/article/details/91227919
Recommended