首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAGradientLayer不工作

CAGradientLayer不工作
EN

Stack Overflow用户
提问于 2015-10-11 01:19:29
回答 1查看 5.7K关注 0票数 12

我创造了一个新项目。我链接了QuartzCore.framework并在ViewController.m中导入了<QuartzCore/QuartzCore.h>

这是密码。

代码语言:javascript
复制
- (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中调用它,但它们都没有工作。我真的不知道我错过了什么。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-11 01:32:57

您的颜色数组应该是NSArray *colorArray = @[(id)colorOne.CGColor, (id)colorTwo.CGColor];,因为colors数组使用的是CGColorRef,而不是UIColor的,这已经够烦人的了-请参阅CGGradientLayer文档

票数 46
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33060648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档