15

我有一个UIView我想要一个径向渐变,我想知道如何去做这个?

4

5 回答 5

18

斯威夫特 3 - @IBDesignable

我解决了卡利斯和亚历山大的答案。我的目标是尽可能简化。例如删除颜色空间和位置 ( nil),以便渐变使用默认值。

如何使用

第1步

创建文件并添加以下代码:import 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步

RadialGradientView在 Storyboard 上,在 Identity Inspector 中将 UIView 设置为上述:自定义类

第 3 步

在 Attributes Inspector 中为渐变设置内部颜色和外部颜色,然后在 Storyboard 上查看更改: 径向渐变

(注意:我将 Storyboard 上的 UIView 制作得足够大,以填满整个

于 2017-01-09T19:47:09.247 回答
16

首先子类化一个 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];

结果:

径向渐变视图

于 2016-02-19T11:32:43.373 回答
6

为此,您需要下拉到 Core Graphics 并使用CGContextDrawRadialGradient

类似的堆栈溢出问题

如何绘制具有径向渐变的扇区(iphone)

如何使用 Core Graphics/iPhone 绘制渐变线(淡入/淡出)?

其他资源

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

http://redartisan.com/2011/05/13/porting-iconapp-core-graphics

他将代码放在 Github 上,并带有一个 UIView 子类,该子类显示了使用 Core Graphics 制作渐变的(相当冗长的)方式:

https://github.com/crafterm/IconApp/blob/master/IconApp/IconView.m

于 2011-11-14T17:43:54.933 回答
6

这是 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()
}
于 2016-10-26T22:02:23.897 回答
1

这是用于 Xamarin.iOS 的 C# 中的 Karlis 答案。在这里,我直接指定颜色,但您当然可以像 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();
    }
}
于 2017-07-28T17:01:58.233 回答