叨逼叨

好久没更新博客了,才几个月,发生了好多事情,处理了好多事情。不变的是写代码依然在继续。

做点啥子

看看objective-c的书,学着写了个柱状图,只是练习的demo而已,iOS上的图表控件已经有非常好的解决方案了。

PNChart:https://github.com/kevinzhow/PNChart

这个控件是是挺不错了,喜欢的朋友可以看看,本文很多地方借鉴了PNChart,就当学习源码也可以

动手动手

先上图先上图,配色直接用PNChart的了,还蛮喜欢这种配色风格,一直不太喜欢把手机横过来,所以做了个竖版的,还有很多不完善,学的时间也短,大家别见笑哈~

ps:附带赠送双色球号码一注,大家可以去买,万一中了呢,由于女朋友老爹热爱福利事业,就写一个给他老人家玩玩,具体实现有人需要在贴吧,哈哈,我就是这么骗回复的

               

相关知识点

毕竟是新手入门的东西,列一下相关的知识点,如果后面有时间再一个个展开吧

OC语法方面:

NSArray,NSString,@interface,@property,@nonatomic,@implementation,id,alloc等

iOS方面:

UIView,CAShapeLayer,UIBezierPath,CATextLayer,UIColor,CABasicAnimation

撸代码

言归正传,看看代码实现部分

首先需要定义LZBar.h,包含了基本的声明,当然还缺少了X轴的文字说明,大家可以自己扩展下:

 #import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h> @interface LZBar : UIView{
CAShapeLayer *backgroundLayer; //背景层
UIBezierPath *backgroundPath; //背景赛贝尔路径
CAShapeLayer *barLayer; //柱状层
UIBezierPath *barPath; //柱状赛贝尔路径
CATextLayer *textLayer; //数值文字显示层
CATextLayer *tittleLayer; //标题文字说明层
} @property (nonatomic) UIColor *backgroundColor;//背景色
@property (nonatomic) UIColor *barColor;//柱的颜色
@property (nonatomic) float barProgress;//柱子长度 0-1之间
@property (nonatomic) float barWidth;//柱子宽度
@property (nonatomic) NSString *barText;//数值
@property (nonatomic) NSString *barTittle;//标题 @end
 #import "LZBar.h"

 @implementation LZBar

 //初始化
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
backgroundLayer = [CAShapeLayer new];
[self.layer addSublayer:backgroundLayer];
backgroundLayer.strokeColor = LZGrey.CGColor;
backgroundLayer.frame = self.bounds; barLayer = [CAShapeLayer new];
[self.layer addSublayer:barLayer];
barLayer.strokeColor = LZGreen.CGColor;
barLayer.lineCap = kCALineCapButt;
barLayer.frame = self.bounds; self.barWidth = self.bounds.size.width;
}
return self;
} //设置背景
- (void)setBackground
{
backgroundPath = [UIBezierPath bezierPath];
[backgroundPath moveToPoint:CGPointMake(self.bounds.origin.x, self.bounds.origin.y+self.bounds.origin.y+self.bounds.size.height/)];
[backgroundPath addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.origin.y+self.bounds.origin.y+self.bounds.size.height/)];
[backgroundPath setLineWidth:_barWidth];
[backgroundPath setLineCapStyle:kCGLineCapSquare];
backgroundLayer.path = backgroundPath.CGPath;
} //设置百分百(显示动画)
- (void)setProgress
{
barPath = [UIBezierPath bezierPath];
[barPath moveToPoint:CGPointMake(self.bounds.origin.x, self.bounds.origin.y+self.bounds.origin.y+self.bounds.size.height/)];
[barPath addLineToPoint:CGPointMake(self.bounds.size.width*_barProgress, self.bounds.origin.y+self.bounds.origin.y+self.bounds.size.height/)];
[barPath setLineWidth:_barWidth];
[barPath setLineCapStyle:kCGLineCapSquare]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = @0.0f;
pathAnimation.toValue = @1.0f;
[barLayer addAnimation:pathAnimation forKey:nil]; barLayer.strokeEnd = 1.0; barLayer.path = barPath.CGPath;
} //设置柱子的宽度
- (void)setBarWidth:(float)progressWidth
{
_barWidth = progressWidth;
backgroundLayer.lineWidth = _barWidth;
barLayer.lineWidth = _barWidth; [self setBackground];
[self setProgress];
} //设置背景色
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundLayer.strokeColor = backgroundColor.CGColor;
} //设置柱子颜色
- (void)setBarColor:(UIColor *)barColor
{
barLayer.strokeColor = barColor.CGColor;
} //设置柱子进度
- (void)setBarProgress:(float)progress
{
_barProgress = progress;
[self setProgress];
} //设置数值
- (void)setBarText:(NSString*)text{
textLayer = [CATextLayer layer];
textLayer.string = text;
textLayer.foregroundColor = [[UIColor blackColor] CGColor];
textLayer.fontSize = ;
textLayer.alignmentMode = kCAAlignmentLeft; textLayer.bounds = barLayer.bounds;
textLayer.position = CGPointMake(self.bounds.size.width*/ + , self.bounds.size.height/);
CABasicAnimation *fade = [self fadeAnimation];
[textLayer addAnimation:fade forKey:nil];
[self.layer addSublayer:textLayer];
} //设置标题
- (void)setBarTittle:(NSString*)tittle{
tittleLayer = [CATextLayer layer];
tittleLayer.string = tittle;
tittleLayer.foregroundColor = [[UIColor blackColor] CGColor];
tittleLayer.fontSize = ;
tittleLayer.alignmentMode = kCAAlignmentRight; tittleLayer.bounds = barLayer.bounds;
tittleLayer.position = CGPointMake(-self.bounds.size.width/ - , self.bounds.size.height/);
CABasicAnimation *fade = [self fadeAnimation];
[tittleLayer addAnimation:fade forKey:nil];
[self.layer addSublayer:tittleLayer];
} //渐变动画
-(CABasicAnimation*)fadeAnimation
{
CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeAnimation.toValue = [NSNumber numberWithFloat:1.0];
fadeAnimation.duration = 2.0; return fadeAnimation;
}
@end

有了Bar之后问题就变的简单的多了,我们可以在构建一个Chart,方便我们直接使用,防止内容过长,看起来累,代码我就折叠了~

 #import <UIKit/UIKit.h>

 @interface LZChart : UIView{
CGSize size;//图表大小
} @property (nonatomic)NSArray *numLabels;//值
@property (nonatomic)NSArray *nameLabels;//名称
@property (nonatomic)float maxNum;//最大值 @property (nonatomic)NSInteger barSpacing;//两根柱状图的间距 @property (nonatomic) CGFloat chartMarginLeft;
@property (nonatomic) CGFloat chartMarginRight;
@property (nonatomic) CGFloat chartMarginTop;
@property (nonatomic) CGFloat chartMarginBottom; - (void)show;//现实图标 @end

LZChart.h

 #import "LZChart.h"
#import "LZBar.h" @implementation LZChart -(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
size = frame.size;
_chartMarginTop = 30.0;
_chartMarginBottom = 30.0;
_chartMarginLeft = 30.0;
_chartMarginRight = 30.0;
_barSpacing = ;
}
return self;
} -(void)show{
[self setMaxNum]; float barCount = [_numLabels count];
float barMaxWidth = size.width - _chartMarginLeft - _chartMarginRight ;
float barHeight = (size.height - _chartMarginTop - _chartMarginBottom) / barCount - _barSpacing;
//防止柱状图太粗
if(barHeight > ){
barHeight = ;
}
float barWidth = ; for(int i = ;i<barCount;i++){
LZBar *bar = [[LZBar alloc] initWithFrame:CGRectMake(_chartMarginLeft, _chartMarginTop + i*(barHeight + _barSpacing), barMaxWidth, barHeight)];
barWidth = [_numLabels[i] floatValue];
bar.barProgress = barWidth/_maxNum;
bar.barWidth = barHeight;
bar.barText = [NSString stringWithFormat:@"%.1f",barWidth];
bar.barTittle = [NSString stringWithFormat:@"%@",_nameLabels[i]];
[self addSubview:bar];
}
} -(void)setMaxNum{
_maxNum = ;
for (id num in _numLabels) {
if ([num floatValue] > _maxNum) {
_maxNum = [num floatValue] ;
}
}
}
@end

LZChart.m

然后在需要添加的UIView直接调用,是不是很容易呢

     LZChart *chart = [[LZChart alloc] initWithFrame:CGRectMake(, , , )];
chart.numLabels = [NSArray arrayWithObjects:@,@,@,@, nil];
chart.nameLabels = [NSArray arrayWithObjects:@"第一",@"第二",@"第三",@"第四", nil];
[self.view addSubview:chart];
[chart show];

最终效果:

希望大家喜欢~

博客地址: http://www.cnblogs.com/nightcat/
博客版权: 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如文中有不妥或者错误的地方还望高手的指出,以免误人子弟。如果觉得本文对您有所帮助请【推荐】一下!如果你有更好的建议,不妨留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。

iOS简易柱状图(带动画)--新手入门篇的更多相关文章

  1. 基于Rebound制造绚丽的动画效果-入门篇

    基于Rebound制造绚丽的动画效果-入门篇 Rebound是什么? Rebound是一个来自 Facebook 公司的 Java物理和动画库.Rebound spring 模型可用于创建动画,让你感 ...

  2. Grunt新手入门篇

    今天看到一篇通俗易懂的Grunt入门文章,博主写得很用心,原文请戳:http://yujiangshui.com/grunt-basic-tutorial/ 当时学习 Grunt 的时候,真是很头疼. ...

  3. entity framework 新手入门篇(4)-entity framework扩展之 entityframework.extended

    对于EF的操作,我们已经有了大概的了解了,但对于实战来说,似乎还欠缺着一些常用的功能,那就是批量的删除,更新数据. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和seller ...

  4. entity framework 新手入门篇(1)-建立模型

    entity framework是微软官方免费提供给大家的一套ORM(Object Relational Mapping对象关系映射)解决方案.它不仅可以帮助我们解决数据缓存的问题,还能在最小的开销下 ...

  5. IOS的一个带动画的多项选择的控件(一)

    先上效果图: 这个程序分2个层次,一个是顶部的带UITextField的bar,一个是下拉选择的view,下拉选择的view带有4个自己定义的UIView 我们先定义一个UIViewControlle ...

  6. MYSQL新手入门篇

    一.数据库的简介 什么是数据库? 数据的仓库,如:在atm的实例中我们创建一个db目录称之为数据库 什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 他们 ...

  7. entity framework 新手入门篇(3)-entity framework实现orderby,count,groupby,like,in,分页等

    前面我们已经学习了entityframework的基本的增删改查,今天,我们将在EF中实现一些更加贴近于实际功能的SQL方法. 承接上面的部分,我们有一个叫做House的数据库,其中包含house表和 ...

  8. entity framework 新手入门篇(2)-entity framework基本的增删改查

    经过前两节的简单描述,终于可以进入entity framework的使用部分了.本节将对entity framework原生的增删改查进行讲解. 承接上面的部分,我们有一个叫做House的数据库,其中 ...

  9. entity framework 新手入门篇(1.5)-lambda表达式与linq

    在建立好了EF模型之后,先不着急使用它,在使用它之前,你还需要了解两个相关的技术,lambda表达式与linq. 作为微软C#语言中重要的语法糖-lambda表达式与LINQ,本质都是一个方法,以la ...

随机推荐

  1. JPA一对一关联

    这里我们仍然是使用annotation对实体进行配置.使用person与idcard模拟一对一的关联关系,一个人只能有一个ID号,同样一个ID号只能对应一个人,人与ID号是一对一的关联关系.Perso ...

  2. jdbc至sql server的两种常见方法

    Statement和prepareStatement sql server中已建立BookPhone数据库,包含bookPhone表,eclipse中有BookPhone类,三个string类型的值 ...

  3. tmpFile.renameTo(classFile) failed 错误

    突然的出现了这个tmpFile.renameTo(classFile) failed 错误. 也许是我删掉了tomcat里面的webapps 中的项目,而通过debug部署,而出现了这个问题. 一开始 ...

  4. C/C++ 动态存储分配

    C语言的动态分配函数: malloc(m):开辟m字节长度的地址空间,并返回这段空间的首地址 sizeof(x):计算变量x的长度 free(p):释放指针p所指变量的存储空间,即彻底删除一个变量 C ...

  5. Uva-11374-Airport Express

    A - Airport Express Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Appoi ...

  6. poj1125&zoj1082Stockbroker Grapevine(Floyd算法)

    Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Description Stockbrokers are known to ...

  7. 转载的vim配置文件

    """"""""""""""""&quo ...

  8. Android增加v7 appcompat源码

    1.File ---- Import---- Existing Android Code Into Workspace 2.选择 <sdk>/extras/android/support/ ...

  9. multiprocessing module in python(转)

    序.multiprocessing python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Python提供了非常好用的多进程包mu ...

  10. JavaScript的函数重载

    java语言中函数的重载和重写可谓是很重要的概念,所以在写js的时候时不时的会想到这种用法,重写先不说,这里只说重载.. <script language="JavaScript&qu ...