IOS UI -- CAShapeLayer, ui -- cashapelayer

Source: Internet
Author: User

IOS UI -- CAShapeLayer, ui -- cashapelayer
About CAShapeLayerCAShapeLayer

  • 1. CAShapeLayer inherits from CALayer and can use all attribute values of CALayer.
  • 2. CAShapeLayer must be used with the besell curve to make sense.
  • 3. The CAShapeLayer and besserer curves can be used to draw some desired images in the drawRect method of view.
  • 4. CAShapeLayer belongs to the CoreAnimation framework. Its animation rendering is directly submitted to the GPU of the mobile phone. Compared with the drawRect method of view, CAShapeLayer is highly efficient in CPU rendering, memory usage can be greatly optimized.
    • Personal experience: You can override drawRect In the subclass of UIView to achieve the progress bar effect. However, drawRect of UIView is implemented by CPU rendering, while CAShapeLayer is more efficient because it uses GPU rendering.
Relationship between the beiser curve and CAShapeLayer
  • 1. CAShapeLayer has the word Shape. As the name suggests, it requires a Shape to take effect.
  • 2. You can create a vector-based path using the besell curve.
  • 3. The beiser curve provides the path for CAShapeLayer. CAShapeLayer renders the PATH provided by CAShapeLayer, and the path is closed. Therefore, the Shape is drawn for the path.
  • 4. The beiser curve used for CAShapeLayer is used as the path, and its path is a closed-loop curve that is connected first and end, even if the beiser curve is not a closed-loop Curve

    Figure 2.png

    How to establish the relationship between the beiser curve and CAShapeLayer:

    • Class: CAShapeLayer
    • Attribute: path

      • Value: it can be a beiser curve object.
      UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,200,100)];shape.path = circle.CGPath;
    • Attribute: fillColor: Modify the fill color of the beiser curve.

      • Value: CGColor object
    • Attribute: maskToBounds

      • Value: YES. The part exceeding CAShapeLayer is not displayed.
StrokeStart and strokeEnd Animation
  • 1. Set the fillColor of ShapeLayer to transparent background.
  • 2. Set the lineWidth value.
  • 3. Set the line color
  • 4. Set the strokeStart value to 0 and trigger the implicit animation when the strokeEnd value changes.

  • Class: CAShapeLayer

    • Attribute:

      • StrokeStart
      • StrokeEnd
        • Note: The value of strokeEnd must be greater than that of strokeStart.
        • Range: (0 ~ 1)
      Solution: shapeLayer. strokeStart = valueOne <valueTwo? ValueOne: valueTwo; shapeLayer. strokeStart = valueOne> valueTwo? ValueOne: valueTwo;
    • Attribute:

      • LineWidth
      • The color of the strokeColor line.
        • Value: CGColor
Use CAShapeLayer to implement the Circular progress bar

Circular animation. gif

Source code: CircleProgress source code in CircleProgress

Directly changing the path results does not have an ideal gradient process, so you need to use the core Animation:

 

Comprehensive sample source code see: https://github.com/HeYang123456789/UIView

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.