将渐变应用于使用 AddArc 绘制的非方形形状
我正在使用 CGContextAddArc 在 iPhone 应用程序中绘制几个闭合形状,我想对其应用渐变,但找不到任何好的示例。我发现的所有内容都会在某处剪辑绑定到绘制形状引用 CGRect,但我需要将渐变边界剪辑到非 CGRect 形状。有什么想法/帮助吗?
我使用 xcode 4.2.1 与 Storyboard 和 iOS5,尽管这些形状是通过编程方式在视图中绘制的。
我用来绘制非方形形状的代码:
CGContextRef context = UIGraphicsGetCurrentContext();
//set context-based constants
double widthMiddle = self.frame.size.width/2;
double heightMiddle = self.frame.size.height/2;
double avgDimension = (widthMiddle + heightMiddle) / 2;
float arcRadius = avgDimension * .9;
float innerRadius = avgDimension * .4;
double startAngle = 2 * (sectionNumber - 1) * (M_PI / 3);
double endAngle = (2 * (sectionNumber * (M_PI / 3))) - [sectionSpacing doubleValue];
double interfaceAngle = [sectionSpacing doubleValue] * (innerRadius / arcRadius);
double ratingRadius = innerRadius + ((arcRadius-innerRadius) * percentGood);
double percentInterfaceAngle = interfaceAngle * (1-percentGood);
//NSLog(@"InterfaceAngle and percentInterfaceAngle are: %f/%f", interfaceAngle, percentInterfaceAngle);
//draw grey background shape
CGContextBeginPath(context);
CGContextSetLineWidth(context, [lineWeight doubleValue]);
//CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetRGBStrokeColor(context, .65, .65, .65, 1);
CGContextSetAllowsAntialiasing(context, 1);
//outer arc
CGContextAddArc(context, //context
widthMiddle, //X-value for center point of arc
heightMiddle, //Y-value for center point of arc
arcRadius, //Radius of the arc
startAngle, //start angle in radians
endAngle, //end angle in radians (2pi = full circle)
0); //Clockwise? 1 = true
//inner arc
CGContextAddArc(context, //context
widthMiddle, //X-value for center point of arc
heightMiddle, //Y-value for center point of arc
innerRadius, //Radius of the arc
endAngle - interfaceAngle, //start angle in radians
startAngle + interfaceAngle, //end angle in radians (2pi = full circle)
1); //Clockwise? 1 = true
CGContextClosePath(context);
//CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, .65, .65, .65, 1);
//CGContextSetAlpha(context, .6);
CGContextDrawPath(context, kCGPathFillStroke );
I've got a couple of closed shapes that I'm drawing in an iPhone app using CGContextAddArc, and I'd like to apply a gradient to it but can't find any good examples. Everything I've found that clips bounds to a drawn shape reference CGRect somewhere, but I need to clip the gradient bounds to a non-CGRect shape. Any ideas/help?
I'm using xcode 4.2.1 with storyboard and iOS5, though these shapes are being drawn within a view programmatically.
The code I'm using to draw my non-square shapes:
CGContextRef context = UIGraphicsGetCurrentContext();
//set context-based constants
double widthMiddle = self.frame.size.width/2;
double heightMiddle = self.frame.size.height/2;
double avgDimension = (widthMiddle + heightMiddle) / 2;
float arcRadius = avgDimension * .9;
float innerRadius = avgDimension * .4;
double startAngle = 2 * (sectionNumber - 1) * (M_PI / 3);
double endAngle = (2 * (sectionNumber * (M_PI / 3))) - [sectionSpacing doubleValue];
double interfaceAngle = [sectionSpacing doubleValue] * (innerRadius / arcRadius);
double ratingRadius = innerRadius + ((arcRadius-innerRadius) * percentGood);
double percentInterfaceAngle = interfaceAngle * (1-percentGood);
//NSLog(@"InterfaceAngle and percentInterfaceAngle are: %f/%f", interfaceAngle, percentInterfaceAngle);
//draw grey background shape
CGContextBeginPath(context);
CGContextSetLineWidth(context, [lineWeight doubleValue]);
//CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetRGBStrokeColor(context, .65, .65, .65, 1);
CGContextSetAllowsAntialiasing(context, 1);
//outer arc
CGContextAddArc(context, //context
widthMiddle, //X-value for center point of arc
heightMiddle, //Y-value for center point of arc
arcRadius, //Radius of the arc
startAngle, //start angle in radians
endAngle, //end angle in radians (2pi = full circle)
0); //Clockwise? 1 = true
//inner arc
CGContextAddArc(context, //context
widthMiddle, //X-value for center point of arc
heightMiddle, //Y-value for center point of arc
innerRadius, //Radius of the arc
endAngle - interfaceAngle, //start angle in radians
startAngle + interfaceAngle, //end angle in radians (2pi = full circle)
1); //Clockwise? 1 = true
CGContextClosePath(context);
//CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, .65, .65, .65, 1);
//CGContextSetAlpha(context, .6);
CGContextDrawPath(context, kCGPathFillStroke );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
CGContextClip
函数,或者可能是CGContextEOClip
函数。CGContextClip 将上下文的剪切路径设置为其当前剪切路径与其当前路径的交集,然后清除当前路径。 CGContextEOClip 执行相同的操作,但使用不同的方式处理路径中的“漏洞”。仅当您有一条与自身相交的路径或包含多个闭合子路径时,差异才有意义。
You are looking for the
CGContextClip
function, or maybe theCGContextEOClip
function.CGContextClip
sets the context's clipping path to the intersection of its current clipping path and its current path, and then clears the current path.CGContextEOClip
does the same, but with a different way of handling "holes" in the path. The different only matters if you have a path that intersects itself, or contains multiple closed subpaths.