Search code examples
objective-clayercashapelayer

Draw line CAShapeLayer layer color by rect region


The Problem I facing right now is that after I draw the dots and lines by CAShapeLayer, I need to modify both of their colors by changing with different for rect regions. The regions I set right now have four portions, and following are the sample codes:

- (void)drawChartWithRecord:(Chart *)Chart
                           lineWidth:(CGFloat)lineWidth
                              radius:(CGFloat)radius
                            interval:(CGFloat)interval
                                rect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);        
    float start_angle = 0;
    float end_angle = (float) (2 * M_PI);

    CAShapeLayer *avg = [CAShapeLayer layer];
    avg.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(X,Y) radius:radius startAngle:start_angle endAngle:end_angle clockwise:YES].CGPath;

    CGContextMoveToPoint(context, X, Y);
    CGContextAddLineToPoint(context, pastX, pastY);
    CGContextSetStrokeColorWithColor(context, LINE_COLOR);
    CGContextSetLineWidth(context, lineWidth);
    CGContextStrokePath(context);

    [self.layer addSublayer:avg];
}

Upon with the line and dots codes; however, I didn't find out any method to import the rect to the same CAShapeLayer. If I make a new layer as

CAShapeLayer *mask = [CAShapeLayer layer];

There would be a problem that If I use

[self.layer addSublayer:mask];

Two of the sublayers would mix, and I don't want to change my background color.

Thanks for everyone's help : )


Solution

  • Unfortunately, I was wrong about my question.

    The thing I want could be easy to solve by the CAGradientLayer & CAShapeLayer. Once you finish your own CAShapeLayer, by using

    [self.gradientLayer setMask:avgCircle];

    you can easily add the the your own gradient color to the line shape CAShapeLayer

    Be sure add addSublayer: for both CAGradientLayer & CAShapeLayer