Gradient Color To CGContextRef

亡梦爱人 提交于 2019-12-24 21:20:09

问题


I am drawing 4 arcs using CGContextRef.I can fill colour but now i want to set gradient color to that arcs.I did it.it is working fine.Now i want to set different color to each arc.Is there any way to do it?please help me.I am attaching my code and screenshot of that arc.Code :

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGFloat comps[] = {1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0};
CGFloat locs[] = {0,1};
CGGradientRef g = CGGradientCreateWithColorComponents(space, comps, locs, 2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat startAngle = 0;
CGFloat endAngle = 90;
CGFloat radius = 100.0;
CGFloat levelWidth = 50;
for (int i = 0; i < 4; i++) {
    CGContextMoveToPoint(context, self.bounds.size.width/2, self.bounds.size.height/2);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius, DEGREES_RADIANS(startAngle), DEGREES_RADIANS(endAngle), NO);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius - levelWidth, DEGREES_RADIANS(endAngle),DEGREES_RADIANS(startAngle), YES);
    startAngle = endAngle;
    endAngle = endAngle + 90;
    CGContextClosePath(context);

}


//CGContextClosePath(context);
CGContextClip(context);
CGContextDrawRadialGradient(context, g, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 1.0f, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 150, 0);

And screen shot :


回答1:


You want to save the context with CGContextSaveGState before you clip, clip and draw the arc, and then restore the context with CGContextRestoreGState before drawing the next arc. And you obviously want an array of CGGradientRef. Thus:

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

CGGradientRef g[4];

CGFloat comps0[] = {1.0,1.0,1.0,1.0,0.0,1.0,1.0,1.0};
CGFloat locs[] = {0,1};
g[0] = CGGradientCreateWithColorComponents(space, comps0, locs, 2);

CGFloat comps1[] = {1.0,1.0,1.0,1.0,1.0,0.0,1.0,1.0};
g[1] = CGGradientCreateWithColorComponents(space, comps1, locs, 2);

CGFloat comps2[] = {1.0,1.0,1.0,1.0,1.0,1.0,0.0,1.0};
g[2] = CGGradientCreateWithColorComponents(space, comps2, locs, 2);

CGFloat comps3[] = {1.0,1.0,1.0,1.0,0.5,0.0,0.5,1.0};
g[3] = CGGradientCreateWithColorComponents(space, comps3, locs, 2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat startAngle = 0;
CGFloat endAngle = 90;
CGFloat radius = 100.0;
CGFloat levelWidth = 50;
for (int i = 0; i < 4; i++) {
    CGContextSaveGState(context);
    CGContextMoveToPoint(context, self.bounds.size.width/2, self.bounds.size.height/2);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius, DEGREES_RADIANS(startAngle), DEGREES_RADIANS(endAngle), NO);
    CGContextAddArc(context, self.bounds.size.width/2, self.bounds.size.height/2, radius - levelWidth, DEGREES_RADIANS(endAngle),DEGREES_RADIANS(startAngle), YES);
    startAngle = endAngle;
    endAngle = endAngle + 90;
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawRadialGradient(context, g[i], CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 1.0f, CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2), 150, 0);
    CGContextRestoreGState(context);
    CGGradientRelease(g[i]);         // don't forget to release the gradient
}



来源:https://stackoverflow.com/questions/18756056/gradient-color-to-cgcontextref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!