设置CALayer渐变背景 [英] Set CALayer Gradient Background

查看:112
本文介绍了设置CALayer渐变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我创建的CALayer添加渐变。我可以使用以下命令设置CALayer的背景颜色:

I am trying to add a gradient to a CALayer I have created. I can set the background colour of the CALayer with the following:

self.colorLayer = [CALayer layer];
[self.colorLayer setBackgroundColor:color.CGColor]; 
[self.colorView setWantsLayer:YES]; 
[self.colorView setLayer:self.colorLayer];

我环顾四周并没有成功(令人惊讶的是,我认为这会被回答很多次)。

I've looked around with no success (surprisingly I thought this would have been answered many times).

我使用了以下渐变色:

NSGradient *gradient = 
    [[NSGradient alloc] initWithStartingColor:[NSColor orangeColor] 
        endingColor:[NSColor lightGrayColor]];

但是无法将其添加到我的CALayer中或添加角度。

But can't add it to my CALayer or add an angle.

推荐答案

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.colors = [NSArray arrayWithObjects:(id)[[NSColor whiteColor] CGColor], (id)[[NSColor greenColor] CGColor], nil];
    gradient.frame = self.colorView.bounds;
    [self.colorView setLayer:gradient];
    [self.colorView setWantsLayer:YES];

这篇关于设置CALayer渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆