使用UIBezierPath快速绘制折线

UIBezierPath
是非常强大的绘图工具,可以使用它绘制包括圆弧,椭圆,直线,虚线,折线等各类线性图形。今天就详细介绍一下使用UIBezierPath绘制折线图。

先看一下绘制后的效果图:

折线图.png

使用UIBezierPath绘制大致分为以下几个步骤:

  1. 获取UIBezierPath对象,设置UIBezierPath的属性,如折线宽度,点与点的连接样式。
  2. 为UIBezierPath对象设置起始点和结束点。
  3. 获取CAShapeLayer 对象,设置layer的绘制颜色,填充颜色。
  4. 将UIBezierPath对象和CAShaperLayer对象进行关联。
  5. 将CAShaperLayer对象添加到需要显示的view上。

结合代码分享一下,从绘制X轴,Y轴和具体的折线的绘制过程, 并为点(x,y)添加响应的点击事件
1. 绘制 x 轴

*  画 X 轴
*/
- (void) drawXAxisView
{
//  因为x轴的数据比较多,需要折线活动,所以将折线绘制在scrollView上,将scrollView添加到当前view上

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 0, self.frame.size.width - 30, self.frame.size.height)];  
_scrollView.contentSize = CGSizeMake((self.frame.size.width - 40) * 2, 0);
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.bounces = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
if (_scrollView.contentInset.top == 64) {
_scrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
}
[self addSubview:_scrollView];
self.xAxisView = [[XAxisView alloc] initWithFrame:CGRectMake(0,self.frame.size.height - 20, _scrollView.contentSize.width, 20)];
[_xAxisView initXAxisValues:_XAxisValuesArray];
[_scrollView addSubview:_xAxisView];  // 将绘制好的x轴添加到scrollView

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,self.frame.size.height - 21, _scrollView.contentSize.width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[_scrollView addSubview:lineView]; // x 轴单位尺的分界线

}```
**2. 绘制Y轴**
```/**
*  画 Y 轴
*/
- (void) drawYAxisView{
self.yAxisView = [[YAxisView alloc] initWithFrame:CGRectMake(0, 0, 40, self.frame.size.height - 20)]; 

[_yAxisView initYAxisValues:_YAxisValuesArray];
[self addSubview:_yAxisView]; // 将绘制好的y轴添加到当前view上

}```
**3.绘制折线**

/**

  • 画折线
    */
  • (void) drawPolylineXValues:(NSArray *)xValues andYValues:(NSArray *)yValues
    {
    self.xValues = xValues;
    self.yValues = yValues;
    UIBezierPath *path = [UIBezierPath bezierPath]; // 1. 获取UIBezierPath对象

path.lineWidth = 1; // 设置UIBezierPath的对象的属性

CGFloat x1 = _xAxisView.xSpaceValue * [xValues[0] floatValue];
CGFloat y1 = self.frame.size.height - _yAxisView.ySpaceValue * [yValues[0] floatValue] - _yAxisView.ySpaceValue;
[path moveToPoint:CGPointMake(x1, y1)]; // 2. UIBezierPath的起始点

[self addButtonAtX:x1 andY:y1 andTag:0]; // 当前点添加事件,方便点击显示相应信息

for (int i = 1; i < xValues.count; i++) {
CGFloat x = _xAxisView.xSpaceValue * [[xValues objectAtIndex:i] floatValue];
CGFloat y = self.frame.size.height - _yAxisView.ySpaceValue * [[yValues objectAtIndex:i] floatValue] - _yAxisView.ySpaceValue;
[path addLineToPoint:CGPointMake(x, y)]; //2. UIBezierPath的结束点

[self addButtonAtX:x andY:y andTag:i];
}
self.shapelayer = [CAShapeLayer layer]; // 3. 获取CAShaperLayer 对象

if (!_lineColor) {
self.lineColor = [UIColor redColor];
}
_shapelayer.strokeColor = _lineColor.CGColor; // 设置 CAShaperLayer对象的属性(折线的颜色)

_shapelayer.fillColor = [[UIColor clearColor]CGColor]; 设置 CAShaperLayer对象的属性

_shapelayer.path = path.CGPath; // 4. UIBezierPath对象和CAShaperLayer对象进行关联

[_scrollView.layer addSublayer:_shapelayer]; // 5. 添加CAShaperLayer到当前view

}```
4.在(x,y)处 画点。

/**

*  画点
*/
- (void) drawPoint{
for (int i = 0; i < _xValues.count; i++) {
CGFloat x = _xAxisView.xSpaceValue * [[_xValues objectAtIndex:i] floatValue];
CGFloat y = self.frame.size.height - _yAxisView.ySpaceValue * [[_yValues objectAtIndex:i] floatValue] - _yAxisView.ySpaceValue;
[self addPointToPolyLine:x andY:y];
}
}
/**

*  开始画点
*
*  @param x x轴值
*  @param y y轴值
*/
- (void) addPointToPolyLine:(CGFloat )x andY:(CGFloat)y{
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:4 startAngle:(0) endAngle:(M_PI*2) clockwise:YES];
self.circleLayer = [CAShapeLayer layer];
_circleLayer.position = CGPointMake(x, y);
_circleLayer.strokeColor = [UIColor whiteColor].CGColor;
_circleLayer.fillColor = [UIColor cyanColor].CGColor;
_circleLayer.lineWidth = 2.0;
_circleLayer.path = circlePath.CGPath;
[_scrollView.layer insertSublayer:_circleLayer above:_shapelayer];
}

5. 在点(x,y) 处添加button, 并且添加点击事件,点击后,会显示当前点的相关信息

/** 

*  在点(x,y)出增加button
*
*  @param button
*/
- (void) addButtonAtX:(CGFloat)x andY:(CGFloat)y andTag:(int)tag
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag = 2000 + tag;
button.frame = CGRectMake(x-10, y-10, 20, 20);
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:button];
}
/**
*  点击(x,y)处的响应事件
*
*/
- (void) clickAction:(UIButton *)button
{
int index = (int)button.tag - 2000;
CGFloat x = _xAxisView.xSpaceValue * [_xValues[index] floatValue];
[self drawVerticalLine:x];
[self removeShowLabel];
CGFloat y = self.frame.size.height - _yAxisView.ySpaceValue * [_yValues[index] floatValue] - _yAxisView.ySpaceValue;
self.label = [[UILabel alloc] init];
_label.text = [NSString stringWithFormat:@"x:%@,y:%@",_xValues[index],_yValues[index]];
_label.frame = CGRectMake(x, y, [self widthWithString:[NSString stringWithFormat:@"x:%@,y:%@",_xValues[index],_yValues[index]] andFont:13.0], 20);
_label.backgroundColor = [UIColor groupTableViewBackgroundColor];
_label.textAlignment = NSTextAlignmentCenter;
_label.font = [UIFont systemFontOfSize:13.0];
_label.layer.borderColor = [UIColor lightGrayColor].CGColor;
_label.layer.borderWidth = 0.5;
[_scrollView addSubview:_label];
}
/**
*  画直线
*/
- (void) drawVerticalLine:(CGFloat) x
{
for (int i = 0; i < _scrollView.layer.sublayers.count; i++) {
if ([_scrollView.layer.sublayers[i] isEqual:_verticalLayer]) {
[_verticalLayer removeFromSuperlayer];
}
}
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 1;
[path moveToPoint:CGPointMake(x, 0)];
[path addLineToPoint:CGPointMake(x, self.frame.size.height - 22)];
self.verticalLayer = [CAShapeLayer layer];
_verticalLayer.strokeColor = [[UIColor cyanColor]CGColor];
_verticalLayer.fillColor = [[UIColor clearColor]CGColor];
_verticalLayer.path = path.CGPath;
[_scrollView.layer addSublayer:_verticalLayer];
}

6. 画水平方向的虚线。

/**

*  画虚线
*/
- (void) drawDashLine
{
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 1;
for (int i = 1; i < _YAxisValuesArray.count; i++) {
CGFloat x = _xAxisView.xSpaceValue * [[_XAxisValuesArray lastObject] floatValue];
CGFloat y = self.frame.size.height - _yAxisView.ySpaceValue * [[_YAxisValuesArray objectAtIndex:i] floatValue] - _yAxisView.ySpaceValue;
[path moveToPoint:CGPointMake(0, y)];
[path addLineToPoint:CGPointMake(x, y)];
}
CAShapeLayer *dashLinelayer = [CAShapeLayer layer];
dashLinelayer.strokeColor = [[UIColor grayColor]CGColor];
dashLinelayer.fillColor = [[UIColor clearColor]CGColor];
dashLinelayer.path = path.CGPath;
dashLinelayer.lineDashPattern = @[@2,@2]; // 斜线的长度

[_scrollView.layer insertSublayer:dashLinelayer below:_shapelayer];
}

以下方法为类 XAxisView,和YAxisVieW中的方法。

/**

*  初始化X轴,类XAxisView中的方法
*   
*/
- (void) initXAxisValues:(NSArray *)xAxisValuesArray  
{
self.xSpaceValue = self.frame.size.width/xAxisValuesArray.count; // x 轴单位尺的距离
// 增加X轴值
for (int i = 0; i < xAxisValuesArray.count; i++) {
if (i != 0) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i * _xSpaceValue - _xSpaceValue/2, 8, _xSpaceValue, 12)];
label.textColor = [UIColor blackColor];
label.textAlignment  = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [xAxisValuesArray objectAtIndex:i];
[self addSubview:label];
}
UIView *view = [[UIView alloc] initWithFrame:CGRectMake((i + 1) * _xSpaceValue, 0, 1, 6)];
view.backgroundColor = [UIColor blackColor];
[self addSubview:view];
}
}
/**

*  初始化Y轴,类YAxisView中的方法

*
*/
- (void) initYAxisValues:(NSArray *)yAxisValuesArray   
{
self.ySpaceValue = self.frame.size.height/yAxisValuesArray.count; // y 轴单位尺的距离
UIView *ylineView = [[UIView alloc] initWithFrame:CGRectMake(39, 0, 1, self.frame.size.height)];
ylineView.backgroundColor = [UIColor blackColor];
[self addSubview:ylineView];
// 增加分割线
for (int i = 1; i < yAxisValuesArray.count; i++) {
UIView *seperatorLineView = [[UIView alloc] initWithFrame:CGRectMake(34, i * _ySpaceValue - 0.5, 5, 1)];
seperatorLineView.backgroundColor = [UIColor blackColor];
[self addSubview:seperatorLineView];
}
// 增加Y轴值
for (int i = 0; i < yAxisValuesArray.count; i++) 
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - (i * (_ySpaceValue)) - 6, 30, 12)];
label.textColor = [UIColor blackColor];
label.textAlignment  = NSTextAlignmentRight;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [yAxisValuesArray objectAtIndex:i];
[self addSubview:label];
}
}

以上就是使用UIBezierPath绘制折线的大概过程,如有任何不足和缺点,欢迎指教。O(∩_∩)O

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容