ios 如何更改 CAGradientLayer 色点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20387803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change CAGradientLayer color points?
提问by rob mayoff
Right now I've got a GA Gradient layer where I've set the colors but I'd like to set the color point to (instead of top center, to top left) and the bottom to (instead of bottom center to bottom right) just to change things up a bit. Thoughts? Below is the code that I've got so far...I included core animation because I'm animation between colors.
现在我有一个 GA Gradient 图层,我已经在其中设置了颜色,但我想将颜色点设置为(而不是顶部中心,左上角)和底部(而不是底部中心到右下角) ) 只是为了稍微改变一下。想法?下面是我到目前为止的代码......我包含了核心动画,因为我是颜色之间的动画。
- (id)init {
self = [super init];
UIView *gradientView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.w, self.h)];
[self addSubview:gradientView];
[self sendSubviewToBack:gradientView];
topColor = [UIColor colorWithRed:0.012 green:0.012 blue:0.012 alpha:1];
bottomColor = [UIColor colorWithRed:1.000 green:0.765 blue:0.235 alpha:1];
gradient = [CAGradientLayer layer];
gradient.frame = gradientView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.7], nil];
[gradientView.layer addSublayer:gradient];
[self performSelector:@selector(animateColors) withObject:self afterDelay:2.0];
currentColorCount = 1;
return self;
}
On the right (What I've got) on the left (what I'd like)
在右边(我有什么)在左边(我想要什么)
回答by rob mayoff
The startPoint
and endPoint
properties of a CAGradientLayer
are defined in the “unit coordinate system”. In the unit coordinate system:
a 的startPoint
和endPoint
属性CAGradientLayer
在“单位坐标系”中定义。在单位坐标系中:
(0,0)
corresponds to the smallest coordinates of the layer's bounds rectangle, which on iOS is its upper-left corner unless the layer has been transformed;(1,1)
corresponds to the largest coordinates of the layer's bounds rectangle, which on iOS is its lower-right corner unless the layer has been transformed.
(0,0)
对应于图层边界矩形的最小坐标,在 iOS 上是它的左上角,除非图层已经被变换;(1,1)
对应于图层边界矩形的最大坐标,在 iOS 上是其右下角,除非图层已被转换。
Thus arranging your gradient the way you want should be this simple:
因此,按照您想要的方式排列渐变应该很简单:
gradient.startPoint = CGPoint.zero
gradient.endPoint = CGPoint(x: 1, y: 1)