我在viewDidLoad的后台有一个CAGradientLayer绘图。它有时会起作用,但有时在我重新启动计算机之前,它不会持久地呈现任何内容。我不明白为什么它有时可以工作,有时不能,它可以工作,比如说连续5个版本,然后它就会停止渲染。没有错误。有人有这方面的经验吗?
背景层方法:
+ (CAGradientLayer*) morningGradient {
UIColor *mornTop = [UIColor colorWithRed:0.843 green:0.722 blue:0.667 alpha:1.000];
UIColor *mornBottom = [UIColor colorWithRed:0.584 green:0.733 blue:0.945 alpha:1.000];
NSArray *colors = [NSArray arrayWithObjects:(id)mornTop.CGColor, mornBottom.CGColor, nil];
NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
NSNumber *stopTwo = [NSNumber numberWithFloat:0.7];
NSNumber *stopThree = [NSNumber numberWithFloat:1.0];
NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, nil];
CAGradientLayer *headerLayer = [CAGradientLayer layer];
headerLayer.colors = colors;
headerLayer.locations = locations;
return headerLayer;
}Draw方法:
-(void)drawGrad
{
NSLog(@"drawing gradient");
CAGradientLayer *bgLayer = [BackgroundLayer morningGradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}ViewDidLoad:
- (void)viewDidLoad
{
[self drawGrad];
[super viewDidLoad];
}发布于 2013-04-17 19:00:55
从CGGradientCreateWithColors(colorSpace, colors, locations[])的文档中,您可以阅读
locations数组应包含与colors数组相同的项数。
我假设 CAGradientLayer 也是如此,但是在文档中找不到任何东西。我做这个假设是因为它有意义。你将如何真正解释两种颜色和三个位置?第三个位置的颜色应该是什么?
更改您的代码,以便传递与位置相同数量的颜色。
发布于 2013-04-17 20:40:37
创建一个自定义渐变视图与大小为3的圆角矩形,您需要添加QuartzCore框架,然后遵循以下代码(.h文件和.m文件)
#import <UIKit/UIKit.h>
@interface CustomGradientView : UIView
@end
#import "CustomGradientView.h"
#import <QuartzCore/QuartzCore.h>
@implementation CustomGradientView
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color1=[UIColor whiteColor];
CGColorRef startColor =color1.CGColor;
UIColor *color2=[UIColor redColor];
CGColorRef endColor = color2.CGColor;
drawLinearGradient(context, rect, startColor, endColor);
CGPathRef p = [[UIBezierPath bezierPathWithRoundedRect:rect
cornerRadius:3] CGPath];
CGContextAddRect(context, rect);
CGContextAddPath(context, p);
CGContextEOClip(context);
CGContextClearRect(context, rect);
}
@endhttps://stackoverflow.com/questions/16057204
复制相似问题