UIBezierPath with color gradient

前端 未结 5 1653
孤街浪徒
孤街浪徒 2021-02-08 15:21

I\'ve got an question about UIBezierPath.

For example I\'ve got this path:

\"\"

Now I want to have

5条回答
  •  借酒劲吻你
    2021-02-08 15:52

    you can give it a try :)

    - (void)drawRect:(CGRect)rect
    {
        CGFloat arcStep = (M_PI *2) / 360; // M_PI*2 is equivalent of full cirle
        BOOL clocklwise = NO;
        CGFloat x = CGRectGetWidth(rect) / 2; // circle's center
        CGFloat y = CGRectGetHeight(rect) / 2; // circle's center
        CGFloat radius = MIN(x, y) / 2;
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        // draw colorful circle
        CGContextSetLineWidth(ctx, radius*2);
        for (CGFloat i = 0; i < 360; i+=1)
        {
            UIColor* c = [UIColor colorWithHue:i/360 saturation:1. brightness:1. alpha:1];
    
            CGContextSetStrokeColorWithColor(ctx, c.CGColor);
    
            CGFloat startAngle = i * arcStep;
            CGFloat endAngle = startAngle + arcStep + 0.02;
    
            CGContextAddArc(ctx, x, y, radius, startAngle, endAngle, clocklwise);
            CGContextStrokePath(ctx);
        }
        // drawing circles then, you might want few of them - smaller radius and less alpha with each step
        UIColor* c = [[UIColor whiteColor] colorWithAlphaComponent: 0.03];
        for (CGFloat fillRadius = radius/2; fillRadius > 0; fillRadius -= 1.f)
        {
            CGContextSetLineWidth(ctx, fillRadius*2);
            CGContextSetStrokeColorWithColor(ctx, c.CGColor);
            CGContextAddArc(ctx, x, y, fillRadius, 0, M_PI * 2, clocklwise);
            CGContextStrokePath(ctx);
        }
    }
    

提交回复
热议问题