0

我在MTAnimatedLabel中看到了代码

 CAGradientLayer *gradientLayer  = (CAGradientLayer *)self.layer;
gradientLayer.backgroundColor   = [super.textColor CGColor];
gradientLayer.startPoint        = CGPointMake(-self.gradientWidth, 0.);
gradientLayer.endPoint          = CGPointMake(0., 0.);
gradientLayer.colors            = [NSArray arrayWithObjects:(id)[self.textColor CGColor],(id)[self.tint CGColor], (id)[self.textColor CGColor], nil];

自我是一个UILabel。从文档中,该层应该是只读的,但我不知道为什么在这个 repo 中它可以工作。

所以我在我的程序中尝试这些代码。

 UIView *gradientView = [[UIView alloc] initWithFrame:textLayer.frame];
[self.view addSubview:gradientView];
CAGradientLayer *gradient = (CAGradientLayer*)gradientView.layer;
gradient.startPoint = CGPointMake(-0.4, 0.0);
gradient.endPoint = CGPointMake(0.0, 0.0);
gradient.colors = @[(id)[UIColor darkGrayColor].CGColor, (id)[UIColor whiteColor].CGColor, (id)[UIColor darkGrayColor].CGColor];
gradient.backgroundColor = [UIColor clearColor].CGColor;
gradient.frame = textLayer.frame

它抛出一个错误: -[CALayer setColors:]: unrecognized selector sent to instance 0x8f8dca0

我不明白为什么我的代码无法运行。希望任何人都可以帮助我。

4

1 回答 1

2

looks you missed add

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

to make view layer gradient, by default this method return CALayer, that not have colors property see UIView class reference

于 2014-02-27T16:17:06.257 回答