ios - 在 iOS 开发中,使用 Core Graphics 和/或 Quartz 2D,如何以看起来像球体的方式绘制一个充满渐变的圆?

标签 ios objective-c geometry core-graphics quartz-2d

到目前为止,我已经研究过使用 CGContextDrawLinearGradient() 和 CGContextDrawRadialGradient(),但是,对于前者,我不知道如何使渐变看起来像一个球体,而对于后者,我也不知道如何将渐变变成球体的形状,因为每次我尝试时,结果都是整个圆柱体的形状,而不仅仅是一个球体。

我当前用来绘制二维圆的代码如下所示。我想使用渐变或任何其他可能的方法,仅使用 Core Graphics 和/或 Quartz 2D 以使其看起来像球体的方式填充圆。

当前画圆的代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();
float x = 20.0;
float y = 20.0;
float radius = 12.5;
CGContextFillEllipseInRect(ctx, CGRectMake(x, y, radius, radius);

附言- 上面的代码按预期工作,所以如果有任何错误,它们只是我在输入问题时的错误,而不是在实际文件中输入代码时的错误。

最佳答案

我相信这就是您要寻找的效果:

enter image description here

这是使用径向渐变创建的。渐变以 0 的半径开始,以圆圈大小的半径结束。起点的中心点必须在终点创建的圆圈内,否则您将得到一个圆锥形。这是我用来制作这张图片的代码(在使用之前需要将几个部分翻译成 iOS):

CGContextRef ctxt = [[NSGraphicsContext currentContext] graphicsPort];
CGGradientRef gradient;
CGColorSpaceRef colorSpace;
CGFloat locations[] = {0.0,1.0};
CGFloat components[] = { 0.5,1.0,1.0,1.0, 0.25,0.5,0.5,1.0 };
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
gradient = CGGradientCreateWithColorComponents(colorSpace,components,locations,
                                               sizeof(locations)/sizeof(CGFloat));
CGPoint start = {70.0,130.0}, end = {100.0,100.0};
CGFloat startRadius = 0.0, endRadius = 90.0;
CGContextDrawRadialGradient(ctxt,gradient,start,startRadius,end,endRadius,0);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);

关于ios - 在 iOS 开发中,使用 Core Graphics 和/或 Quartz 2D,如何以看起来像球体的方式绘制一个充满渐变的圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6638934/

相关文章:

ios - iOS 应用的照片尺寸和分辨率

objective-c - iOS 上事件监视器中的实时字节与实际内存

objective-c - 属性(property)继承 : Auto property synthesis will not synthesize property

objective-c - TDD 对于 2-3 个月大小且期限紧迫的小型 iOS/Android 移动应用程序项目的效果如何?

math - 快速放大三角形的方法

ios - 从带有导航 Controller 的 xib 过渡

ios - viewDidLoad/viewDidAppear 的 UIView 动画?

c++ - 通过快速用户切换检测 Mac OS X 中事件 session 的 API

python - 为椭圆体内的 3D 数组中的点赋值

c# - C# 中图像上的水印?