在iOS开发中,如果使用Core Graphics和/或Quartz 2D,我怎样才能绘制出一个充满渐变的圆,使其看起来像一个球体? [英] In iOS Development, using Core Graphics and/or Quartz 2D, how can I draw a circle filled with a gradient in such a manner that it looks like a sphere?

查看:150
本文介绍了在iOS开发中,如果使用Core Graphics和/或Quartz 2D,我怎样才能绘制出一个充满渐变的圆,使其看起来像一个球体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经看过使用CGContextDrawLinearGradient()和CGContextDrawRadialGradient(),但是,前者我无法弄清楚如何使渐变看起来像一个球体,而后者,我无法弄清楚如何让渐变变成球体的形状,因为每次尝试时,都会变成完整圆柱体的形状,而不是只有一个球体。



下面显示了我正在使用的当前绘制2D圆的代码。我想使用渐变或任何其他方法,只使用Core Graphics和/或Quartz 2D来填充圆,使其看起来像一个球体。



当前用于绘制圆圈的代码:

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

上面的代码就像它应该的那样工作,所以如果有任何错误,他们只是从我的错误输入问题,而不是在输入代码到实际文件中。

解决方案

我相信这是您正在寻找的效果:


这是使用径向渐变创建的,渐变以半径为0并以圆的大小半径结束,起点的中心点必须位于由结束点创建的圆内,否则将得到一个圆锥形状,下面是我用来制作此图像的代码(a几个部分需要在使用之前转换到iOS):

  CGContextRef ctxt = [[NSGraphicsContext currentContext] graphicsPort]; 
CGGradientRef梯度;
CGC olorSpaceRef colorSpace;
CGFloat locations [] = {0.0,1.0};
CGFloat组件[] = {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);


So far I have looked at using CGContextDrawLinearGradient() and CGContextDrawRadialGradient(), however, with the former I can't figure out how to make the gradient look like a sphere, and with the latter, I can't figure out how to make the gradient into the shape of a sphere, because every time I try, it turns out in the shape of the full cylinder instead of only a sphere.

The code I am using to current draw a 2D circle is shown below. I would like to use a gradient or any other method possible using only Core Graphics and/or Quartz 2D to fill the circle in a manner that makes it look like a sphere.

Current code for drawing a circle:

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

P.S. - The above code works just as its supposed to, so in case there are any errors, they're just from my errors while typing the question, not while typing code into the actual file.

解决方案

I believe this is the effect you are looking for:

This is created using a radial gradient. The gradient starts with a radius of 0 and ends with a radius of the size of the circle. The center point of the start must be within the circle created by the end, or you will get a cone shape instead. Here is the code I used to make this image (a couple parts need to be translated to iOS before you use it):

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开发中,如果使用Core Graphics和/或Quartz 2D,我怎样才能绘制出一个充满渐变的圆,使其看起来像一个球体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆