CAGradientLayer不工作
我创build了一个新的项目。 我在ViewController.m
链接QuartzCore.framework
并导入了<QuartzCore/QuartzCore.h>
。
这里是代码。
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"view height %f", self.view.frame.size.height); // returns 667 on iPhone 6 NSLog(@"view width %f", self.view.frame.size.width); // returns 375 on iPhone 6 NSLog(@"layers count %lu", self.view.layer.sublayers.count); // returns 2 // Gradient UIColor *colorOne = [UIColor blueColor]; UIColor *colorTwo = [UIColor greenColor]; NSArray *colorArray = @[colorOne, colorTwo]; NSNumber *locationOne = [NSNumber numberWithFloat:0.0]; NSNumber *locationTwo = [NSNumber numberWithFloat:1.0]; NSArray *locationArray = @[locationOne, locationTwo]; CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = self.view.frame; gradientLayer.colors = colorArray; gradientLayer.locations = locationArray; [self.view.layer insertSublayer:gradientLayer atIndex:0]; //[self.view.layer insertSublayer:gradientLayer above:[self.view.layer.sublayers firstObject]]; // didn't work either NSLog(@"layers count %lu", self.view.layer.sublayers.count); //returns 3 }
我试图设置视图的背景颜色clearColor
,在viewDidAppear
调用它,但没有一个工作。 我真的不知道我错过了什么。 谢谢你的帮助。
你的颜色数组应该是NSArray *colorArray = @[(id)colorOne.CGColor, (id)colorTwo.CGColor];
,因为colors
数组需要CGColorRef
,而不是UIColor
,令人讨厌的 – 看到CGGradientLayer文档