我创建了一个新项目。在LinkedIn中QuartzCore.framework
并进口<QuartzCore/QuartzCore.h>
in the ViewController.m
.
这是代码。
- (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
s,而不是UIColor
s,够烦人的 - 看看CGGradientLayer 文档 https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CAGradientLayer_class/#//apple_ref/occ/instp/CAGradientLayer/startPoint
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)