If I have two UIColors, what's the best way to draw an even gradient between them over an arbitrarily-sized area?
如果我有两个UIColors,在一个任意大小的区域中,在它们之间画一个均匀梯度的最好方法是什么?
I am guessing you would create a UIView subclass and use the CG drawing methods in the drawRect method. I just don't know which ones, or if there are any pitfalls to watch out for, like performance. Has anyone done this?
我猜你会创建一个UIView子类并在drawRect方法中使用CG绘图方法。我只是不知道是哪一个,或者如果有什么陷阱要注意,比如性能。有人做过吗?
7 个解决方案
#1
20
You'll want to use CGGradient. See the iPhone Dev Center "CGGradient Reference" document.
你需要使用CGGradient。参见iPhone开发中心“CGGradient参考”文档。
#2
50
#import <QuartzCore/QuartzCore.h>
- (void) setGradient {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
}
Don't forget to add QuartzCore library to your project.
不要忘记在项目中添加QuartzCore库。
#3
3
The Swift way to add Gradient is :
快速增加渐变的方法是:
var view : UIView = UIView(frame: CGRectMake(0, 0, 320, 100))
var g : CAGradientLayer = CAGradientLayer()
g.frame = gradientView.bounds
g.colors = ["000000".UIColor.CGColor , "111111".UIColor.CGColor]
view.layer.insertSublayer(g, atIndex: 0)
UIColor()
is a helper class for Swift to convert hex color
to rgb color
, highly recommended.
UIColor()是快速转换hex颜色到rgb颜色的助手类,强烈推荐。
#4
0
Same-ish solution as @Anton Chikin but a little more robust. Notice the override of layerClass
... this way, you don't have to worry about setting the frame and the frame is automatically updated upon rotation & resize.
同样的解决方案是@Anton Chikin,但是有点健壮。请注意layerClass的覆盖…这样,您不必担心设置框架和框架会自动更新在旋转和调整大小。
class GradientView: UIView {
var colorA : UIColor = UIColor.greenColor() {
didSet { updateGradient() }
}
var colorB : UIColor = UIColor.blueColor() {
didSet { updateGradient() }
}
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateGradient()
}
func updateGradient() {
if let gLayer = layer as? CAGradientLayer {
gLayer.colors = [colorA.CGColor, colorB.CGColor]
}
}
}
If you're using IB, you can set the properties via "User Defined Runtime Attributes".
如果使用IB,则可以通过“用户定义的运行时属性”设置属性。
If you're not using IB, use the other initializer.
如果您没有使用IB,请使用其他初始化器。
#5
0
I found that using CAGradientLayer produced a gradient with noticeable stepping Ended up using this
我发现使用CAGradientLayer会产生一个渐变,并且使用这个渐变效果。
http://evilrockhopper.com/tag/cggradient/
http://evilrockhopper.com/tag/cggradient/
As per this question Gradients on UIView and UILabels On iPhone
根据这个问题,UIView和UILabels在iPhone上的梯度。
#6
0
hi you can use following to to apply gradient on view
你好,你可以使用以下的方法在视图上应用渐变。
-(void)applyGradientEffecttoView:(UIView *)aView
{
// Create the colors
UIColor *darkOp = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0];
UIColor *lightOp =[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:0.0];
// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
gradient.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5],
nil];
// Set bounds
gradient.frame = aView.bounds;
// Add the gradient to the view
[aView.layer insertSublayer:gradient atIndex:0];
}
#7
0
For views that draw text, such as UILabel
, UITextView
, and UITextField
, you should add the gradient inside the drawRect
function. (see this post for an example).
对于绘制文本的视图,如UILabel、UITextView和UITextField,应该在drawRect函数中添加渐变。(参见本文中的示例)。
There are some great subclasses of UILabel
, UITextView
, and UITextField
that allow you to add a gradient pretty easily (see JBGradient on GitHub).
UILabel、UITextView和UITextField有一些很好的子类,可以很容易地添加一个渐变(见GitHub上的jb渐变)。
#1
20
You'll want to use CGGradient. See the iPhone Dev Center "CGGradient Reference" document.
你需要使用CGGradient。参见iPhone开发中心“CGGradient参考”文档。
#2
50
#import <QuartzCore/QuartzCore.h>
- (void) setGradient {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[self.view.layer insertSublayer:gradient atIndex:0];
}
Don't forget to add QuartzCore library to your project.
不要忘记在项目中添加QuartzCore库。
#3
3
The Swift way to add Gradient is :
快速增加渐变的方法是:
var view : UIView = UIView(frame: CGRectMake(0, 0, 320, 100))
var g : CAGradientLayer = CAGradientLayer()
g.frame = gradientView.bounds
g.colors = ["000000".UIColor.CGColor , "111111".UIColor.CGColor]
view.layer.insertSublayer(g, atIndex: 0)
UIColor()
is a helper class for Swift to convert hex color
to rgb color
, highly recommended.
UIColor()是快速转换hex颜色到rgb颜色的助手类,强烈推荐。
#4
0
Same-ish solution as @Anton Chikin but a little more robust. Notice the override of layerClass
... this way, you don't have to worry about setting the frame and the frame is automatically updated upon rotation & resize.
同样的解决方案是@Anton Chikin,但是有点健壮。请注意layerClass的覆盖…这样,您不必担心设置框架和框架会自动更新在旋转和调整大小。
class GradientView: UIView {
var colorA : UIColor = UIColor.greenColor() {
didSet { updateGradient() }
}
var colorB : UIColor = UIColor.blueColor() {
didSet { updateGradient() }
}
override class func layerClass() -> AnyClass {
return CAGradientLayer.self
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
updateGradient()
}
func updateGradient() {
if let gLayer = layer as? CAGradientLayer {
gLayer.colors = [colorA.CGColor, colorB.CGColor]
}
}
}
If you're using IB, you can set the properties via "User Defined Runtime Attributes".
如果使用IB,则可以通过“用户定义的运行时属性”设置属性。
If you're not using IB, use the other initializer.
如果您没有使用IB,请使用其他初始化器。
#5
0
I found that using CAGradientLayer produced a gradient with noticeable stepping Ended up using this
我发现使用CAGradientLayer会产生一个渐变,并且使用这个渐变效果。
http://evilrockhopper.com/tag/cggradient/
http://evilrockhopper.com/tag/cggradient/
As per this question Gradients on UIView and UILabels On iPhone
根据这个问题,UIView和UILabels在iPhone上的梯度。
#6
0
hi you can use following to to apply gradient on view
你好,你可以使用以下的方法在视图上应用渐变。
-(void)applyGradientEffecttoView:(UIView *)aView
{
// Create the colors
UIColor *darkOp = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0];
UIColor *lightOp =[UIColor colorWithRed:255.0f green:255.0f blue:255.0f alpha:0.0];
// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
gradient.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.5],
nil];
// Set bounds
gradient.frame = aView.bounds;
// Add the gradient to the view
[aView.layer insertSublayer:gradient atIndex:0];
}
#7
0
For views that draw text, such as UILabel
, UITextView
, and UITextField
, you should add the gradient inside the drawRect
function. (see this post for an example).
对于绘制文本的视图,如UILabel、UITextView和UITextField,应该在drawRect函数中添加渐变。(参见本文中的示例)。
There are some great subclasses of UILabel
, UITextView
, and UITextField
that allow you to add a gradient pretty easily (see JBGradient on GitHub).
UILabel、UITextView和UITextField有一些很好的子类,可以很容易地添加一个渐变(见GitHub上的jb渐变)。