叨逼叨

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

做点啥子

看看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. Linux Shell 04 数字/字符串/文件测试

    一. 数字测试 格式:n1  -op  n2 测试操作op: eq/ne/le/ge/lt/gt    -->    等于/不等于/小于等于/大于等于/小于/大于 1. 数字比较可以使用特殊的( ...

  2. env,export

    这几个命令都是和Linux环境变量相关的 env 查看系统所有的环境变量,个人觉得这里面最重要的就是PATH,我们在终端敲入的命令,其实都是一个个脚本文件,那么Shell去哪找这些文件呢?搜索路径就存 ...

  3. python ljust,rjust,center,zfill对齐使用方法

    字符串在输出时的对齐:S.ljust(width,[fillchar]) #输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格. S.rjust(width,[fillcha ...

  4. Memcache入门

    说来惭愧,第一次听说Memcache是在大约在6个月前.作为一个搞J2EE开发的,工作一年多了,都没听说过Memcache实在是惭愧. 当时是换了新工作,第一个任务是开发一个报表系统供公司内部使用.为 ...

  5. Keepalived高可用集群搭建(转载linuxIDC)

    1.Keepalived简介 Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障.使用多台节点安装keepalived.其 他的节点用来提供真实的服务,同 ...

  6. Hive beeline update

    Hive cli vs beeline The primary difference between the two involves how the clients connect to Hive. ...

  7. UESTC 882 冬马党 --状压DP

    定义:dp[i][j]为状态为j时,第i行符合条件的状态数 转移方程:dp[i][j] += dp[i-1][t]   //t为上一行状态,与当前行不冲突. 从第一行开始向下枚举,每次枚举当前行的状态 ...

  8. MongoDB学习(一)简介

    本篇主要简单介绍一下MongoDB数据库. 一.简介 mongoDB是一个开源的,基于分布式的,面向文档存储的非关系型数据库.是非关系型数据库当中功能最丰富.最像关系数据库的. mongoDB由C++ ...

  9. IO流的练习1 —— 随机获取文本中的信息

    需求:一个文本中有几个名字,随机从中获取一个名字 分析: A:首先把文本中的数据读出 B:再把数据存储到集合中 C:产生一个随机的索引 D:打印出这个索引对应的值 public static void ...

  10. 原生js实现增加(addclass),删除(removeclass),判断是否存在(hasclass),如果存在删除,如果不存在添加(toggleclass)和获取类名(getbyclass)的方法

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...