Core Animation - 渐变色CAGradientLayer
原创
©著作权归作者所有:来自51CTO博客作者CodingFight的原创作品,请联系作者获取转载授权,否则将追究法律责任
由一个颜色过渡到另一个颜色,甚至直接作出彩虹的效果,这便是CAGradientLayer的作用,CAGradientLayer有startPoint和endPoint两个属性,分别是{0,0}和{1,1}:
//create gradient layer and add it to our container view
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.containerView.bounds;
[self.containerView.layer addSublayer:gradientLayer];
//set gradient colors
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
//set gradient start and end points
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
除了渐变还有多重渐变这个概念,就是我上面说的彩虹色,当然,只要你愿意,彩虹色也是可以写出来的,设置locations这个属性:
gradientLayer.locations = @[@0.0, @0.25, @0.5];
来给颜色进行分段,但是博主用了这个感觉并不好用,所以博主试着在gradientLayer.colors这个颜色数组中分别加入了彩虹的每一个颜色:
//彩虹色
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor orangeColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor greenColor].CGColor, (__bridge id)[UIColor cyanColor].CGColor, (__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor purpleColor].CGColor];
然后炫酷的彩虹色就出来了,github地址:https://github.com/codeliu6572/CAGradientLayer