iOS 动画绘制线条颜色渐变的折线图
效果圖
....................
概述
現(xiàn)狀
折線圖的應(yīng)用比較廣泛,為了增強(qiáng)用戶體驗(yàn),很多應(yīng)用中都嵌入了折線圖。折線圖可以更加直觀的表示數(shù)據(jù)的變化。網(wǎng)絡(luò)上有很多繪制折線圖的demo,有的也使用了動(dòng)畫,但是線條顏色漸變的折線圖的demo少之又少,甚至可以說沒有。該Blog闡述了動(dòng)畫繪制線條顏色漸變的折線圖的實(shí)現(xiàn)方案,以及折線圖線條顏色漸變的實(shí)現(xiàn)原理,并附以完整的示例。- 成果
- 本人已將折線圖封裝到了一個(gè)UIView子類中,并提供了相應(yīng)的接口。該自定義折線圖視圖,基本上可以適用于大部分需要集成折線圖的項(xiàng)目。若你遇到相應(yīng)的需求可以直接將文件拖到項(xiàng)目中,調(diào)用相應(yīng)的接口即可
- 項(xiàng)目文件中包含了大量的注釋代碼,若你的需求與折線圖的實(shí)現(xiàn)效果有差別,那么你可以對項(xiàng)目文件的進(jìn)行修改,也可以依照思路定義自己的折線圖視圖
- Blog中涉及到的知識點(diǎn)
- CALayer
- 圖層,可以簡單的看做一個(gè)不接受用戶交互的UIView
- 每個(gè)圖層都具有一個(gè)CALayer類型mask屬性,作用與蒙版相似
- Blog中主要用到的CALayer子類有
- CAGradientLayer,繪制顏色漸變的背景圖層
- CAShapeLayer,繪制折線圖
- CAAnimation
- 核心動(dòng)畫的基類(不可實(shí)例化對象),實(shí)現(xiàn)動(dòng)畫操作
- Quartz 2D
- 一個(gè)二維的繪圖引擎,用來繪制折線(Path)和坐標(biāo)軸信息(Text)
- CALayer
實(shí)現(xiàn)思路
- 折線圖視圖
- 整個(gè)折線圖將會被自定義到一個(gè)UIView子類中
- 坐標(biāo)軸繪制
- 坐標(biāo)軸直接繪制到折線圖視圖上,在自定義折線圖視圖的 drawRect 方法中繪制坐標(biāo)軸相關(guān)信息(線條和文字)
- 注意坐標(biāo)系的轉(zhuǎn)換
- 線條顏色漸變
- 失敗的方案
- 開始的時(shí)候,為了實(shí)現(xiàn)線條顏色漸變,我的思考方向是,如何改變路徑(UIBezierPath)的渲染顏色(strokeColor)。但是strokeColor只可以設(shè)置一種,所以最終無法實(shí)現(xiàn)線條顏色的漸變。
- 成功的方案
- 在探索過程中找到了CALayer的CALayer類型的mask()屬性,最終找到了解決方案,即:使用UIView對象封裝漸變背景視圖(frame為折線圖視圖的減去坐標(biāo)軸后的frame),創(chuàng)建一個(gè)CAGradientLayer漸變圖層添加到背景視圖上。
- 創(chuàng)建一個(gè)CAShapeLayer對象,用于繪制線條,線條的渲染顏色(strokeColor)為whiteColor,填充顏色(fillColor)為clearColor,從而顯示出漸變圖層的顏色。將CAShapeLayer對象設(shè)置為背景視圖的mask屬性,即背景視圖的蒙版。
- 失敗的方案
- 折線
- 使用 UIBezierPath 類來繪制折線
- 折線轉(zhuǎn)折處尖角的處理,使用 kCALineCapRound 與 kCALineJoinRound 設(shè)置折線轉(zhuǎn)折處為圓角
- 折線起點(diǎn)與終點(diǎn)的圓點(diǎn)的處理,可以直接在 UIBezierPath 對象上添加一個(gè)圓,設(shè)置遠(yuǎn)的半徑為路徑寬度的一半,從而保證是一個(gè)實(shí)心的圓而不是一個(gè)圓環(huán)
- 折線轉(zhuǎn)折處的點(diǎn)
- 折線轉(zhuǎn)折處點(diǎn)使用一個(gè)類來描述(不使用CGPoint的原因是:折線轉(zhuǎn)折處的點(diǎn)需要放到一個(gè)數(shù)組中)
- 坐標(biāo)軸信息
- X軸、Y軸的信息分別放到一個(gè)數(shù)組中
- X軸顯示的是最近七天的日期,Y軸顯示的是最近七天數(shù)據(jù)變化的幅度
- 動(dòng)畫
- 使用CABasicAnimation類來完成繪制折線圖時(shí)的動(dòng)畫
- 需要注意的是,折線路徑在一開始時(shí)需要社會線寬為0,開始繪制時(shí)才設(shè)置為適當(dāng)?shù)木€寬,保證一開折線路徑是隱藏的
- 標(biāo)簽
- 在動(dòng)畫結(jié)束時(shí),向折線圖視圖上添加一個(gè)標(biāo)簽(UIButton對象),顯示折線終點(diǎn)的信息
- 標(biāo)簽的位置,需要根據(jù)折線終點(diǎn)的位置計(jì)算
具體實(shí)現(xiàn)
- 折線轉(zhuǎn)折處的點(diǎn)
使用一個(gè)類來描述折線轉(zhuǎn)折處的點(diǎn),代碼如下:
// 接口 /** 折線圖上的點(diǎn) */ @interface IDLineChartPoint : NSObject /** x軸偏移量 */ @property (nonatomic, assign) float x; /** y軸偏移量 */ @property (nonatomic, assign) float y; /** 工廠方法 */ + (instancetype)pointWithX:(float)x andY:(float)y; @end// 實(shí)現(xiàn) @implementation IDLineChartPoint + (instancetype)pointWithX:(float)x andY:(float)y {IDLineChartPoint *point = [[self alloc] init];point.x = x;point.y = y;return point; } @end
- 自定義折線圖視圖
折線圖視圖是一個(gè)自定義的UIView子類,代碼如下:
// 接口 /** 折線圖視圖 */ @interface IDLineChartView : UIView /** 折線轉(zhuǎn)折點(diǎn)數(shù)組 */ @property (nonatomic, strong) NSMutableArray<IDLineChartPoint *> *pointArray; /** 開始繪制折線圖 */ - (void)startDrawlineChart; @end// 分類 @interface IDLineChartView () @end// 實(shí)現(xiàn) @implementation IDLineChartView // 初始化 - (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {// 設(shè)置折線圖的背景色self.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];}return self; } @end效果如圖
- 繪制坐標(biāo)軸信息
與坐標(biāo)軸繪制相關(guān)的常量
/** 坐標(biāo)軸信息區(qū)域?qū)挾?*/ static const CGFloat kPadding = 25.0; /** 坐標(biāo)系中橫線的寬度 */ static const CGFloat kCoordinateLineWith = 1.0;在分類中添加與坐標(biāo)軸繪制相關(guān)的成員變量
/** X軸的單位長度 */ @property (nonatomic, assign) CGFloat xAxisSpacing; /** Y軸的單位長度 */ @property (nonatomic, assign) CGFloat yAxisSpacing; /** X軸的信息 */ @property (nonatomic, strong) NSMutableArray<NSString *> *xAxisInformationArray; /** Y軸的信息 */ @property (nonatomic, strong) NSMutableArray<NSString *> *yAxisInformationArray;與坐標(biāo)軸繪制相關(guān)的成員變量的get方法
- (CGFloat)xAxisSpacing {if (_xAxisSpacing == 0) {_xAxisSpacing = (self.bounds.size.width - kPadding) / (float)self.xAxisInformationArray.count;}return _xAxisSpacing; } - (CGFloat)yAxisSpacing {if (_yAxisSpacing == 0) {_yAxisSpacing = (self.bounds.size.height - kPadding) / (float)self.yAxisInformationArray.count;}return _yAxisSpacing; } - (NSMutableArray<NSString *> *)xAxisInformationArray {if (_xAxisInformationArray == nil) {// 創(chuàng)建可變數(shù)組_xAxisInformationArray = [[NSMutableArray alloc] init];// 當(dāng)前日期和日歷NSDate *today = [NSDate date];NSCalendar *currentCalendar = [NSCalendar currentCalendar];// 設(shè)置日期格式NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];dateFormatter.dateFormat = @"MM-dd";// 獲取最近一周的日期NSDateComponents *components = [[NSDateComponents alloc] init];for (int i = -7; i<0; i++) {components.day = i;NSDate *dayOfLatestWeek = [currentCalendar dateByAddingComponents:components toDate:today options:0];NSString *dateString = [dateFormatter stringFromDate:dayOfLatestWeek];[_xAxisInformationArray addObject:dateString];}}return _xAxisInformationArray; } - (NSMutableArray<NSString *> *)yAxisInformationArray {if (_yAxisInformationArray == nil) {_yAxisInformationArray = [NSMutableArray arrayWithObjects:@"0", @"10", @"20", @"30", @"40", @"50", nil];}return _yAxisInformationArray; } // 折線圖上的點(diǎn)(重寫get方法,后期需要暴露接口) - (NSMutableArray<IDLineChartPoint *> *)pointArray {if (_pointArray == nil) {_pointArray = [NSMutableArray arrayWithObjects:[IDLineChartPoint pointWithX:1 andY:1], [IDLineChartPoint pointWithX:2 andY:2], [IDLineChartPoint pointWithX:3 andY:1.5], [IDLineChartPoint pointWithX:4 andY:2], [IDLineChartPoint pointWithX:5 andY:4], [IDLineChartPoint pointWithX:6 andY:1], [IDLineChartPoint pointWithX:7 andY:2], nil];}return _pointArray; }繪制坐標(biāo)軸的相關(guān)信息
- (void)drawRect:(CGRect)rect {// 獲取上下文CGContextRef context = UIGraphicsGetCurrentContext();// x軸信息[self.xAxisInformationArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {// 計(jì)算文字尺寸UIFont *informationFont = [UIFont systemFontOfSize:10];NSMutableDictionary *attributes = [NSMutableDictionary dictionary];attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:158/255.0 green:158/255.0 blue:158/255.0 alpha:1.0];attributes[NSFontAttributeName] = informationFont;CGSize informationSize = [obj sizeWithAttributes:attributes];// 計(jì)算繪制起點(diǎn)float drawStartPointX = kPadding + idx * self.xAxisSpacing + (self.xAxisSpacing - informationSize.width) * 0.5;float drawStartPointY = self.bounds.size.height - kPadding + (kPadding - informationSize.height) / 2.0;CGPoint drawStartPoint = CGPointMake(drawStartPointX, drawStartPointY);// 繪制文字信息[obj drawAtPoint:drawStartPoint withAttributes:attributes];}];// y軸[self.yAxisInformationArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {// 計(jì)算文字尺寸UIFont *informationFont = [UIFont systemFontOfSize:10];NSMutableDictionary *attributes = [NSMutableDictionary dictionary];attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:158/255.0 green:158/255.0 blue:158/255.0 alpha:1.0];attributes[NSFontAttributeName] = informationFont;CGSize informationSize = [obj sizeWithAttributes:attributes];// 計(jì)算繪制起點(diǎn)float drawStartPointX = (kPadding - informationSize.width) / 2.0;float drawStartPointY = self.bounds.size.height - kPadding - idx * self.yAxisSpacing - informationSize.height * 0.5;CGPoint drawStartPoint = CGPointMake(drawStartPointX, drawStartPointY);// 繪制文字信息[obj drawAtPoint:drawStartPoint withAttributes:attributes];// 橫向標(biāo)線CGContextSetRGBStrokeColor(context, 231 / 255.0, 231 / 255.0, 231 / 255.0, 1.0);CGContextSetLineWidth(context, kCoordinateLineWith);CGContextMoveToPoint(context, kPadding, self.bounds.size.height - kPadding - idx * self.yAxisSpacing);CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - kPadding - idx * self.yAxisSpacing);CGContextStrokePath(context);}]; }效果如圖
- 漸變背景視圖
在分類中添加與背景視圖相關(guān)的常量
/** 漸變背景視圖 */ @property (nonatomic, strong) UIView *gradientBackgroundView; /** 漸變圖層 */ @property (nonatomic, strong) CAGradientLayer *gradientLayer; /** 顏色數(shù)組 */ @property (nonatomic, strong) NSMutableArray *gradientLayerColors;在初始化方法中添加調(diào)用設(shè)置背景視圖方法的代碼
[self drawGradientBackgroundView];設(shè)置漸變視圖方法的具體實(shí)現(xiàn)
- (void)drawGradientBackgroundView {// 漸變背景視圖(不包含坐標(biāo)軸)self.gradientBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(kPadding, 0, self.bounds.size.width - kPadding, self.bounds.size.height - kPadding)];[self addSubview:self.gradientBackgroundView];/** 創(chuàng)建并設(shè)置漸變背景圖層 *///初始化CAGradientlayer對象,使它的大小為漸變背景視圖的大小self.gradientLayer = [CAGradientLayer layer];self.gradientLayer.frame = self.gradientBackgroundView.bounds;//設(shè)置漸變區(qū)域的起始和終止位置(范圍為0-1),即漸變路徑self.gradientLayer.startPoint = CGPointMake(0, 0.0);self.gradientLayer.endPoint = CGPointMake(1.0, 0.0);//設(shè)置顏色的漸變過程self.gradientLayerColors = [NSMutableArray arrayWithArray:@[(__bridge id)[UIColor colorWithRed:253 / 255.0 green:164 / 255.0 blue:8 / 255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:251 / 255.0 green:37 / 255.0 blue:45 / 255.0 alpha:1.0].CGColor]];self.gradientLayer.colors = self.gradientLayerColors;//將CAGradientlayer對象添加在我們要設(shè)置背景色的視圖的layer層[self.gradientBackgroundView.layer addSublayer:self.gradientLayer]; }效果如圖
- 折線
在分類中添加與折線繪制相關(guān)的成員變量
/** 折線圖層 */ @property (nonatomic, strong) CAShapeLayer *lineChartLayer; /** 折線圖終點(diǎn)處的標(biāo)簽 */ @property (nonatomic, strong) UIButton *tapButton;在初始化方法中添加調(diào)用設(shè)置折線圖層方法的代碼
[self setupLineChartLayerAppearance];設(shè)置折線圖層方法的具體實(shí)現(xiàn)
- (void)setupLineChartLayerAppearance {/** 折線路徑 */UIBezierPath *path = [UIBezierPath bezierPath];[self.pointArray enumerateObjectsUsingBlock:^(IDLineChartPoint * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {// 折線if (idx == 0) {[path moveToPoint:CGPointMake(self.xAxisSpacing * 0.5 + (obj.x - 1) * self.xAxisSpacing, self.bounds.size.height - kPadding - obj.y * self.yAxisSpacing)];} else {[path addLineToPoint:CGPointMake(self.xAxisSpacing * 0.5 + (obj.x - 1) * self.xAxisSpacing, self.bounds.size.height - kPadding - obj.y * self.yAxisSpacing)];}// 折線起點(diǎn)和終點(diǎn)位置的圓點(diǎn)if (idx == 0 || idx == self.pointArray.count - 1) {[path addArcWithCenter:CGPointMake(self.xAxisSpacing * 0.5 + (obj.x - 1) * self.xAxisSpacing, self.bounds.size.height - kPadding - obj.y * self.yAxisSpacing) radius:2.0 startAngle:0 endAngle:2 * M_PI clockwise:YES];}}];/** 將折線添加到折線圖層上,并設(shè)置相關(guān)的屬性 */self.lineChartLayer = [CAShapeLayer layer];self.lineChartLayer.path = path.CGPath;self.lineChartLayer.strokeColor = [UIColor whiteColor].CGColor;self.lineChartLayer.fillColor = [[UIColor clearColor] CGColor];// 默認(rèn)設(shè)置路徑寬度為0,使其在起始狀態(tài)下不顯示self.lineChartLayer.lineWidth = 0;self.lineChartLayer.lineCap = kCALineCapRound;self.lineChartLayer.lineJoin = kCALineJoinRound;// 設(shè)置折線圖層為漸變圖層的maskself.gradientBackgroundView.layer.mask = self.lineChartLayer; }效果如圖(初始狀態(tài)不顯示折線)
- 動(dòng)畫的開始與結(jié)束
動(dòng)畫開始
/** 動(dòng)畫開始,繪制折線圖 */ - (void)startDrawlineChart {// 設(shè)置路徑寬度為4,使其能夠顯示出來self.lineChartLayer.lineWidth = 4;// 移除標(biāo)簽,if ([self.subviews containsObject:self.tapButton]) {[self.tapButton removeFromSuperview];}// 設(shè)置動(dòng)畫的相關(guān)屬性CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];pathAnimation.duration = 2.5;pathAnimation.repeatCount = 1;pathAnimation.removedOnCompletion = NO;pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];// 設(shè)置動(dòng)畫代理,動(dòng)畫結(jié)束時(shí)添加一個(gè)標(biāo)簽,顯示折線終點(diǎn)的信息pathAnimation.delegate = self;[self.lineChartLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; }動(dòng)畫結(jié)束,添加標(biāo)簽
/** 動(dòng)畫結(jié)束時(shí),添加一個(gè)標(biāo)簽 */ - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {if (self.tapButton == nil) { // 首次添加標(biāo)簽(避免多次創(chuàng)建和計(jì)算)CGRect tapButtonFrame = CGRectMake(self.xAxisSpacing * 0.5 + ([self.pointArray[self.pointArray.count - 1] x] - 1) * self.xAxisSpacing + 8, self.bounds.size.height - kPadding - [self.pointArray[self.pointArray.count - 1] y] * self.yAxisSpacing - 34, 30, 30);self.tapButton = [[UIButton alloc] initWithFrame:tapButtonFrame];self.tapButton.enabled = NO;[self.tapButton setBackgroundImage:[UIImage imageNamed:@"bubble"] forState:UIControlStateDisabled];[self.tapButton.titleLabel setFont:[UIFont systemFontOfSize:10]];[self.tapButton setTitle:@"20" forState:UIControlStateDisabled];}[self addSubview:self.tapButton]; }
集成折線圖視圖
- 創(chuàng)建折線圖視圖
添加成員變量
/** 折線圖 */ @property (nonatomic, strong) IDLineChartView *lineCharView;在viewDidLoad方法中創(chuàng)建折線圖并添加到控制器的view上
self.lineCharView = [[IDLineChartView alloc] initWithFrame:CGRectMake(35, 164, 340, 170)]; [self.view addSubview:self.lineCharView];
添加開始繪制折線圖視圖的按鈕
添加成員變量
/** 開始繪制折線圖按鈕 */ @property (nonatomic, strong) UIButton *drawLineChartButton;在viewDidLoad方法中創(chuàng)建開始按鈕并添加到控制器的view上
self.drawLineChartButton = [UIButton buttonWithType:UIButtonTypeSystem]; self.drawLineChartButton.frame = CGRectMake(180, 375, 50, 44); [self.drawLineChartButton setTitle:@"開始" forState:UIControlStateNormal]; [self.drawLineChartButton addTarget:self action:@selector(drawLineChart) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.drawLineChartButton];開始按鈕的點(diǎn)擊事件
// 開始繪制折線圖 - (void)drawLineChart {[self.lineCharView startDrawlineChart]; }
效果如圖
- 創(chuàng)建折線圖視圖
聲明:若需要工程文件,請?jiān)谠u論中聯(lián)系我,非常愿意與廣大技術(shù)愛好者溝通交流。下一篇博客將會介紹如何使用UICollectionView實(shí)現(xiàn)具有簽到功能的日歷
轉(zhuǎn)載于:https://www.cnblogs.com/theDesertIslandOutOfTheWorld/p/5212183.html
總結(jié)
以上是生活随笔為你收集整理的iOS 动画绘制线条颜色渐变的折线图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android学习(二十)Notific
- 下一篇: 春游植物园