demo地址:https://github.com/xiaochaofeiyu/YSCAnimation
有用的话求个star,欢迎建议讨论。git
CAGradientLayer是CALayer的一个特殊子类,用于生成颜色渐变的图层,使用较为方便,下面介绍下它的相关属性:github
CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor blueColor].CGColor]; gradientLayer.locations = @[@0.3, @0.5, @1.0]; gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(1.0, 0); gradientLayer.frame = CGRectMake(0, 100, 300, 100); [self.view.layer addSublayer:gradientLayer];
CAGradientLayer实现渐变标间简单直观,但存在必定的局限性,好比没法自定义整个渐变区域的形状,如环形、曲线形的渐变。函数
iOS Core Graphics中有两个方法用于绘制渐变颜色,CGContextDrawLinearGradient能够用于生成线性渐变,CGContextDrawRadialGradient用于生成圆半径方向颜色渐变。函数能够自定义path,不管是什么形状均可以,原理都是用来作Clip,因此须要在CGContextClip函数前调用CGContextAddPath函数把CGPathRef加入到Context中。
另一个须要注意的地方是渐变的方向,方向是由两个点控制的,点的单位就是坐标。所以须要正确从CGPathRef中找到正确的点,方法固然有不少种看具体实现,本例中,我就是简单得经过调用CGPathGetBoundingBox函数,返回CGPathRef的矩形区域,而后根据这个矩形取两个点,读者能够根据自行需求修改具体代码。spa
1-> 线性渐变code
- (void)drawLinearGradient:(CGContextRef)context path:(CGPathRef)path startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGFloat locations[] = { 0.0, 1.0 }; NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); CGRect pathRect = CGPathGetBoundingBox(path); //具体方向可根据需求修改 CGPoint startPoint = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMidY(pathRect)); CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMidY(pathRect)); CGContextSaveGState(context); CGContextAddPath(context, path); CGContextClip(context); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); CGContextRestoreGState(context); CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //建立CGContextRef UIGraphicsBeginImageContext(self.view.bounds.size); CGContextRef gc = UIGraphicsGetCurrentContext(); //建立CGMutablePathRef CGMutablePathRef path = CGPathCreateMutable(); //绘制Path CGRect rect = CGRectMake(0, 100, 300, 200); CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMaxY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect)); CGPathCloseSubpath(path); //绘制渐变 [self drawLinearGradient:gc path:path startColor:[UIColor greenColor].CGColor endColor:[UIColor redColor].CGColor]; //注意释放CGMutablePathRef CGPathRelease(path); //从Context中获取图像,并显示在界面上 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; [self.view addSubview:imgView]; }
2-> 圆半径方向渐变对象
- (void)drawRadialGradient:(CGContextRef)context path:(CGPathRef)path startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGFloat locations[] = { 0.0, 1.0 }; NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations); CGRect pathRect = CGPathGetBoundingBox(path); CGPoint center = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect)); CGFloat radius = MAX(pathRect.size.width / 2.0, pathRect.size.height / 2.0) * sqrt(2); CGContextSaveGState(context); CGContextAddPath(context, path); CGContextEOClip(context); CGContextDrawRadialGradient(context, gradient, center, 0, center, radius, 0); CGContextRestoreGState(context); CGGradientRelease(gradient); CGColorSpaceRelease(colorSpace); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //建立CGContextRef UIGraphicsBeginImageContext(self.view.bounds.size); CGContextRef gc = UIGraphicsGetCurrentContext(); //建立CGMutablePathRef CGMutablePathRef path = CGPathCreateMutable(); //绘制Path CGRect rect = CGRectMake(0, 100, 300, 200); CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMaxY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect)); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMinY(rect)); CGPathCloseSubpath(path); //绘制渐变 [self drawRadialGradient:gc path:path startColor:[UIColor greenColor].CGColor endColor:[UIColor redColor].CGColor]; //注意释放CGMutablePathRef CGPathRelease(path); //从Context中获取图像,并显示在界面上 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; [self.view addSubview:imgView]; }
CALayer的mask属性能够做为遮罩让layer显示mask遮住(非透明)的部分;CAShapeLayer为CALayer的子类,经过path属性能够生成不一样的形状,将CAShapeLayer对象用做layer的mask属性的话,就能够生成不一样形状的图层。故生成颜色渐变有如下几个步骤:图片
- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.firstCircle]; _firstCircle.frame = CGRectMake(0, 0, 200, 200); _firstCircle.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2.0, CGRectGetHeight(self.view.bounds) / 2.0); CGFloat firsCircleWidth = 5; self.firstCircleShapeLayer = [self generateShapeLayerWithLineWidth:firsCircleWidth]; _firstCircleShapeLayer.path = [self generateBezierPathWithCenter:CGPointMake(100, 100) radius:100].CGPath; _firstCircle.layer.mask = _firstCircleShapeLayer; } - (CAShapeLayer *)generateShapeLayerWithLineWidth:(CGFloat)lineWidth { CAShapeLayer *waveline = [CAShapeLayer layer]; waveline.lineCap = kCALineCapButt; waveline.lineJoin = kCALineJoinRound; waveline.strokeColor = [UIColor redColor].CGColor; waveline.fillColor = [[UIColor clearColor] CGColor]; waveline.lineWidth = lineWidth; waveline.backgroundColor = [UIColor clearColor].CGColor; return waveline; } - (UIBezierPath *)generateBezierPathWithCenter:(CGPoint)center radius:(CGFloat)radius { UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO]; return circlePath; } - (UIImageView *)firstCircle { if (!_firstCircle) { self.firstCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circleBackground"]]; _firstCircle.layer.masksToBounds = YES; _firstCircle.alpha = 1.0; } return _firstCircle; }
做者:小超飞鱼
连接:http://www.jianshu.com/p/3e0e25fd9b85
來源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。ip