我正在尝试做一些应该非常简单的事情。我想给我的一个视图添加渐变。我可以将其添加到self.view,但不能添加到带有渐变的view。代码如下:
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *colorFirst = [UIColor colorWithWhite:0.10 alpha:0.15];
UIColor *colorLast = [UIColor colorWithWhite:0.535 alpha:0.8];
NSArray *colors = [NSArray arrayWithObjects:(id)colorFirst.CGColor, colorLast.CGColor, nil];
gradient.colors = colors;
NSNumber *stopFirst = [NSNumber numberWithFloat:0.00];
NSNumber *stopLast = [NSNumber numberWithFloat:1.00];
NSArray *locations = [NSArray arrayWithObjects:stopFirst, stopLast, nil];
gradient.locations = locations;
gradient.frame = CGRectMake(0.0, viewWithGradient.frame.origin.y, viewWithGradient.frame.size.width, height_of_gradient);
[self.view.layer addSublayer:gradient]; // this works. I get a nice gradient at
// the bottom of the view, where I want it,
// but I need the gradient further down in
// my view hierarchy.
// [viewWithGradient.layer addSublayer:gradient]; // this doesn't. There
// is no visually noticeable change
// if this is uncommented and the
// above line is.
viewWithGradient是viewController(self.view)中我的主视图中的一个小视图。此视图上只有一个具有渐变的其他视图。这是一个UILabel,它只占Gradient视图面积的一半左右。它有一个透明的背景,但标签以白色绘制文本。我需要让渐变在UILabel下,而不是在self.view上,在UILabel上。
我目前怀疑我的帧/边界可能会将渐变置于屏幕之外。有人看到我丢失的东西吗?这似乎是最简单的CALayer用法,我花了太多时间在这上面。