ios 如何向 UIView 添加径向渐变?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8125623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a radial gradient to a UIView?
提问by Andrew
I have a UIView
which I want to have a radial gradient, and I'm wondering how to go about doing this?
我有一个UIView
我想要一个径向渐变,我想知道如何去做这个?
采纳答案by John Gallagher
To do this you need to drop down to Core Graphics and use CGContextDrawRadialGradient.
为此,您需要下拉到 Core Graphics 并使用CGContextDrawRadialGradient。
Similar Stack Overflow Questions
类似的堆栈溢出问题
How can I draw a sector with radial gradient (iphone)
How to draw a gradient line (fading in/out) with Core Graphics/iPhone?
如何使用 Core Graphics/iPhone 绘制渐变线(淡入/淡出)?
Other Resources
其他资源
There's a tutorial showing how to draw icons with gradients on iOS here:
有一个教程展示了如何在 iOS 上绘制带有渐变的图标:
http://redartisan.com/2011/05/13/porting-iconapp-core-graphics
http://redartisan.com/2011/05/13/porting-iconapp-core-graphics
He's put the code on Github, complete with a UIView subclass that shows the (rather longwinded) way of making gradients using Core Graphics:
他将代码放在 Github 上,并附带一个 UIView 子类,该子类显示了使用 Core Graphics 制作渐变的(相当冗长的)方式:
https://github.com/crafterm/IconApp/blob/master/IconApp/IconView.m
https://github.com/crafterm/IconApp/blob/master/IconApp/IconView.m
回答by Karlis
First subclass a 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
And then add it to your view:
然后将其添加到您的视图中:
UIRadialView *radialView = [[UIRadialView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
radialView.backgroundColor = [UIColor redColor];
[self.view addSubview:radialView];
Result:
结果:
回答by Mark Moeykens
Swift 3 - @IBDesignable
Swift 3 - @IBDesignable
I worked off Karlis and Alexander's answers. I aimed to simplify as much as possible. Such as removing color space and locations (nil
) so the gradient uses the defaults.
我解决了卡利斯和亚历山大的答案。我的目标是尽可能地简化。例如删除颜色空间和位置 ( nil
) 以便渐变使用默认值。
How to Use
如何使用
Step 1
第1步
Create file and add this code: import UIKit
创建文件并添加以下代码: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)
}
}
Step 2
第2步
On Storyboard, set the UIView to the above RadialGradientView
in Identity Inspector:
在 Storyboard 上,RadialGradientView
在 Identity Inspector中将 UIView 设置为上述内容:
Step 3
第 3 步
Set the Inside Color and Outside Color for your gradient in Attributes Inspector and see the change on your Storyboard:
在属性检查器中为您的渐变设置内部颜色和外部颜色,然后查看故事板上的更改:
(Note: I made the UIView on the Storyboard big enough to so it fills the entire
(注意:我在 Storyboard 上制作了足够大的 UIView,因此它填满了整个
回答by Alexander
Here's Karlis' answer in Swift 3:
这是卡利斯在 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()
}
回答by c.lamont.dev
Here's Karlis answer in c# for Xamarin.iOS. Here I am specifying the colors directly but you can of course implement it the same way Karlis did.
这是 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();
}
}