如何制作带有渐变色或多种颜色的CALayer边框?
问题描述:
CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor redColor].CGColor;
rightBorder.borderWidth = 1;
rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)+2);
[self.layer addSublayer:rightBorder];
我正在像上面那样制作WebView的边框.这里self是从UIWebView继承的.
I am making border of a WebView like above. here self is inherited from UIWebView.
如何更改
rightBorder.borderColor = [UIColor redColor].CGColor;
为渐变色,所以我一半的颜色应该是蓝色,一半是白色.
to a Gradient color, so half of my color should be blue, and half white.
在这里,我将红色应用到WebView的边框上.但是我想要多色(2种颜色)或渐变.
Here, I am applying on RED color to border of my WebView. However I want a multicolor (2 color) or gradient.
谢谢.
答
要在具有渐变效果的一层中显示不同的颜色,我要共享一种代码方法,为此您必须使用CAGradientLayer,
To show different color in one layer with gradient effect i am sharing one method of my code you have to use CAGradientLayer for that,
-(void)addGradiantColor:(UIView *)view;
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = view.frame;
// gradientLayer.startPoint = CGPointMake(0.0,0.0);
// gradientLayer.endPoint = CGPointMake(1.0,1.0);
NSMutableArray *colors = [NSMutableArray array];
[colors addObject:(id)[[UIColor colorWithRed:134.0/255.0 green: 234.0/255.0 blue:63.0/255.0 alpha:1.0] CGColor]];
[colors addObject:(id)[[UIColor colorWithRed:215.0/255.0 green: 82.0/255.0 blue:76.0/255.0 alpha:1.0] CGColor]];
gradientLayer.colors = colors;
[view.layer addSublayer:gradientLayer];
}