将渐变应用于使用 AddArc 绘制的非方形形状

发布于 2024-12-26 05:04:59 字数 2936 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2025-01-02 05:04:59

您正在寻找 CGContextClip 函数,或者可能是 CGContextEOClip 函数。

CGContextClip 将上下文的剪切路径设置为其当前剪切路径与其当前路径的交集,然后清除当前路径。 CGContextEOClip 执行相同的操作,但使用不同的方式处理路径中的“漏洞”。仅当您有一条与自身相交的路径或包含多个闭合子路径时,差异才有意义。

You are looking for the CGContextClip function, or maybe the CGContextEOClip 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文