您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 實現漸變色的三種方法總結

編輯:iOS 開發綜合

在 iOS 開發過程中,有時會需要用到漸變色。這篇文章總結了三種實現方式,需要的朋友一起來看看。

一、以 CAGradientLayer 實現漸變

CAGradientLayer 是 CALayer 的一個特殊子類,用於生成顏色漸變的圖層,使用相當方便。先來介紹它的相關屬性:

      colors:漸變的顏色
      locations:漸變顏色的分割點
      startPoint 與 endPoint:顏色漸變的方向,範圍在 (0,0) 與 (1.0,1.0) 之間,如 (0,0)(1.0,0) 代表水平方向漸變,(0,0)(0,1.0) 代表垂直方向漸變

 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 實現漸變簡單直觀,但有局限,例如無法自定義整個漸變區域的形狀(如環形、曲線形漸變)。

二、以 Core Graphics 相關方法實現漸變

iOS Core Graphics 中有兩個方法用於繪製漸變顏色:CGContextDrawLinearGradient 用於生成線性漸變,CGContextDrawRadialGradient 用於生成圓半徑方向顏色漸變。這兩個函數可自定義 path,無論什麼形狀都行,原理都是拿來做 Clip,所以要在 CGContextClip 函數前呼叫 CGContextAddPath 把 CGPathRef 加入 Context。

另外要注意漸變方向由兩個點控制,點的單位就是座標,因此需要正確地從 CGPathRef 中找到合適的點。本例中簡單地透過 CGPathGetBoundingBox 取得 CGPathRef 的矩形區域,再依此取兩個點,讀者可依需求自行修改。

1、線性漸變

 - (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]; }

三、以 CAShapeLayer 作為 layer 的 mask 屬性

CALayer 的 mask 屬性可作為遮罩,讓 layer 只顯示被 mask 遮住(非透明)的部分;CAShapeLayer 是 CALayer 的子類,透過 path 屬性能生成不同形狀,把它作為 layer 的 mask 屬性,就能生成不同形狀的圖層。

因此生成顏色漸變的步驟如下:

     1、建立一個 imageView(也可以是 layer),image 屬性設為漸變色圖片
     2、建立一個 CAShapeLayer 物件,透過 path 屬性指定所需形狀
     3、把 CAShapeLayer 物件賦值給 imageView 的 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; }

總結

以上就是這三種實現 iOS 漸變色的方法,分別是 CAGradientLayer、Core Graphics 繪製,以及以 CAShapeLayer 作為 mask。希望對各位 iOS 開發者有所幫助,有疑問歡迎留言交流。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved