Ios 如何向UIView添加径向渐变?

Ios 如何向UIView添加径向渐变?,ios,objective-c,uiview,radial-gradients,Ios,Objective C,Uiview,Radial Gradients,我有一个ui视图,我想有一个径向梯度,我想知道怎么做?要做到这一点,你需要下拉到核心图形并使用 类似的堆栈溢出问题 其他资源 这里有一个教程展示了如何在iOS上绘制渐变图标: 他将代码放在Github上,并完成了一个UIView子类,该子类展示了使用核心图形制作渐变的(相当冗长的)方法: UIView的第一个子类: @implementation UIRadialView - (void)drawRect:(CGRect)rect { // Setup view CGFl

我有一个
ui视图
,我想有一个径向梯度,我想知道怎么做?

要做到这一点,你需要下拉到核心图形并使用

类似的堆栈溢出问题

其他资源

这里有一个教程展示了如何在iOS上绘制渐变图标:

他将代码放在Github上,并完成了一个UIView子类,该子类展示了使用核心图形制作渐变的(相当冗长的)方法:


UIView的第一个子类:

@implementation UIRadialView

- (void)drawRect:(CGRect)rect
{
    // Setup view
    CGFloat colorComponents[] = {0.0, 0.0, 0.0, 1.0,   // First color:  R, G, B, ALPHA (currently opaque black)
                                 0.0, 0.0, 0.0, 0.0};  // Second color: R, G, B, ALPHA (currently transparent black)
    CGFloat locations[] = {0, 1}; // {0, 1) -> from center to outer edges, {1, 0} -> from outer edges to center
    CGFloat radius = MIN((self.bounds.size.height / 2), (self.bounds.size.width / 2));
    CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);

    // Prepare a context and create a color space
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create gradient object from our color space, color components and locations
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorComponents, locations, 2);

    // Draw a gradient
    CGContextDrawRadialGradient(context, gradient, center, 0.0, center, radius, 0);
    CGContextRestoreGState(context);

    // Release objects
    CGColorSpaceRelease(colorSpace);
    CGGradientRelease(gradient);
}

@end
然后将其添加到视图中:

UIRadialView *radialView = [[UIRadialView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
radialView.backgroundColor = [UIColor redColor];
[self.view addSubview:radialView];
结果:


以下是Karlis在Swift 3中的回答:

override func draw(_ rect: CGRect) {

    // Setup view
    let colors = [UIColor.white.cgColor, UIColor.black.cgColor] as CFArray
    let locations = [ 0.0, 1.0 ] as [CGFloat]
    let radius = min((self.bounds.size.height / 2), (self.bounds.size.width / 2))
    let center = CGPoint.init(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)

    // Prepare a context and create a color space
    let context = UIGraphicsGetCurrentContext()
    context!.saveGState()
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    // Create gradient object from our color space, color components and locations
    let gradient = CGGradient.init(colorsSpace: colorSpace, colors: colors, locations: locations)

    // Draw a gradient
    context!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: radius, options: CGGradientDrawingOptions(rawValue: 0))
    context?.restoreGState()
}
Swift 3-@IB可设计 我根据Karlis和Alexander的答案工作。我的目标是尽可能地简化。例如删除颜色空间和位置(
nil
),以便渐变使用默认值

如何使用 第一步 创建文件并添加以下代码: 导入UIKit

@IBDesignable
class RadialGradientView: UIView {

    @IBInspectable var InsideColor: UIColor = UIColor.clear
    @IBInspectable var OutsideColor: UIColor = UIColor.clear

    override func draw(_ rect: CGRect) {
        let colors = [InsideColor.cgColor, OutsideColor.cgColor] as CFArray
        let endRadius = min(frame.width, frame.height) / 2
        let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
        let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
        UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
    }
}
步骤2 在情节提要上,将UIView设置为Identity Inspector中的上述
RadialGradientView

步骤3 在属性检查器中设置渐变的内部颜色和外部颜色,并在情节提要上查看更改:

(注意:我在故事板上制作了足够大的UIView,以填充整个

这里是Karlis用c#为Xamarin.iOS提供的答案。这里我直接指定了颜色,但您当然可以像Karlis那样实现它

public class RadialView : UIView
{
    public RadialView(CGRect rect) : base (rect)
    {
        this.BackgroundColor = UIColor.DarkGray;
    }

    public override void Draw(CGRect rect)
    {
        CGColor[] colorComponents = { UIColor.DarkGray.CGColor, UIColor.LightGray.CGColor };
        var locations = new nfloat[]{ 1, 0 }; 
        var radius = this.Bounds.Size.Height / 2;
        CGPoint center = new CGPoint(this.Bounds.Size.Width / 2, this.Bounds.Size.Height / 2);

        var context = UIGraphics.GetCurrentContext();
        context.SaveState();
        var colorSpace = CGColorSpace.CreateDeviceRGB();

        CGGradient gradient = new CGGradient(colorSpace, colorComponents, locations);
        context.DrawRadialGradient(gradient, center, 0, center, radius, CGGradientDrawingOptions.None);

        context.RestoreState();
        colorSpace.Dispose();
        gradient.Dispose();
    }
}

这是一个stackoverflow答案的链接,带有如何添加径向渐变的示例代码:链接不再处于活动状态。请不要发布多个问题。发布一个好的答案,然后投票/标记以重复方式关闭其他问题。如果问题不是重复的,请根据问题定制您的答案。我明白了,@PaulRoub,可以这对我很有帮助:让endRadius=sqrt(pow(frame.width/2,2)+pow(frame.height/2,2))