IOS 开发中画扇形图实例详解

昨天在做项目中,遇到一个需要显示扇形图的功能,网上搜了一下,发现code4app里面也没有找到我想要的那种类似的效果,没办法了,只能自己学习一下如何画了。
首先我们需要了解一个uiview的方法
-(void)drawRect:(CGRect)rect
我们知道了这个方法,就可以在自定义UIView的子类的- (void)drawRect:(CGRect)rect里面绘图了,关于drawrect的调用周期,网上也是一找一大堆,等下我会整理一下,转载一篇供你们参考。
废话少说,下面直接开始代码
首先我们自定义一个继承字uiview的子类,我这里就起名字叫pieview了
首先我们试试先画一个圆
#import "pieview.h"
//直径,其实radius是半径的意思吧,哈哈 算了先用着,demo都写好了就不改了,你们知道就行了
#define radius 50
@implementation pieview
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();//获取图形上下文
CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);//设置图形开始画的坐标原点,根据实际需要设置,我这是随便写的
CGContextAddEllipseInRect(ctx, CGRectMake(cent.x, cent.y, 100, 100));这个是核心函数,在这里设置图形的开始从哪里画,画的宽度和高度是多少。如果宽高不一样 可就是椭圆了啊
[[UIColor greenColor] set];//设置颜色
CGContextFillPath(ctx);//实心的
//CGContextStrokePath(ctx);空心的
}
@end
然后我们创建一个控制器 pieViewController 引用我们的pieview,展示一下效果
#import "pieViewController.h"
//#import "myview.h"
//#import "JYpieview.h"
#import "pieview.h"
@interface pieViewController ()
@end
@implementation pieViewController
- (void)viewDidLoad {
[super viewDidLoad];
pieview *view=[[pieview alloc]init];
view.frame=CGRectMake(4, 150, 150, 300);
[self.view addSubview:view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
好了看一下效果吧
好了,下面让我们开始扇形图的制作吧
#import "pieview.h"
//直径
#define radius 50
#define PI 3.14159265358979323846
@implementation pieview
//计算度转弧度
static inline float radians(double degrees) {
return degrees * PI / 180;
}
-(void)drawRect:(CGRect)rect
{
CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
float angle_start = radians(0.0);
float angle_end = radians(120.0);
CGContextMoveToPoint(ctx, cent.x, cent.y);
CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
CGContextAddArc(ctx, cent.x, cent.y, radius, angle_start, angle_end, 0);
CGContextFillPath(ctx);
angle_start = angle_end;
angle_end = radians(360.0);
CGContextMoveToPoint(ctx, cent.x, cent.y);
CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor blueColor] CGColor]));
CGContextAddArc(ctx, cent.x, cent.y, radius, angle_start, angle_end, 0);
CGContextFillPath(ctx);
}
@end
在运行一下,我们看下效果
可使有没有觉得上面的代码很多重复的?对的,我们可以封装一个方法 那么重构后的代码我就一次性的贴上去了
#import "pieview.h"
//直径
#define radius 50
#define PI 3.14159265358979323846
@implementation pieview
//计算度转弧度
static inline float radians(double degrees) {
return degrees * PI / 180;
}
static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) {
CGContextMoveToPoint(ctx, point.x, point.y);
CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0);
//CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
-(void)drawRect:(CGRect)rect
{
CGPoint cent=CGPointMake((self.frame.size.width/2)-radius/2, (self.frame.size.height/2)-radius/2);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
float angle_start = radians(0.0);
float angle_end = radians(121.0);
drawArc(ctx, cent, angle_start, angle_end, [UIColor yellowColor]);
angle_start = angle_end;
angle_end = radians(228.0);
drawArc(ctx, cent, angle_start, angle_end, [UIColor greenColor]);
angle_start = angle_end;
angle_end = radians(260);
drawArc(ctx, cent, angle_start, angle_end, [UIColor orangeColor]);
angle_start = angle_end;
angle_end = radians(360);
drawArc(ctx, cent, angle_start, angle_end, [UIColor purpleColor]);
}
@end
看下运行效果图
如果我们中途数据变了 想要改一下图形怎么办呢?
那么我们就需要用到这个
//通知自定义的view重新绘制图形 // [self setNeedsDisplay];
这时候我们就要pieview向外界提供一个接口属性,这是我做的模拟5面之后改变圆形的直径大小
.h文件
#import <UIKit/UIKit.h> @interface pieview : UIView //直径 @property(nonatomic,assign)float radius; @end
.m文件
#import "pieview.h"
#define PI 3.14159265358979323846
@implementation pieview
//计算度转弧度
static inline float radians(double degrees) {
return degrees * PI / 180;
}
static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color,float radius) {
CGContextMoveToPoint(ctx, point.x, point.y);
CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));
CGContextAddArc(ctx, point.x, point.y, radius, angle_start, angle_end, 0);
//CGContextClosePath(ctx);
CGContextFillPath(ctx);
}
-(void)drawRect:(CGRect)rect
{
CGPoint cent=CGPointMake((self.frame.size.width/2)-self.radius/2, (self.frame.size.height/2)-self.radius/2);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);
float angle_start = radians(0.0);
float angle_end = radians(121.0);
drawArc(ctx, cent, angle_start, angle_end, [UIColor yellowColor],self.radius);
angle_start = angle_end;
angle_end = radians(228.0);
drawArc(ctx, cent, angle_start, angle_end, [UIColor greenColor],self.radius);
angle_start = angle_end;
angle_end = radians(260);
drawArc(ctx, cent, angle_start, angle_end, [UIColor orangeColor],self.radius);
angle_start = angle_end;
angle_end = radians(360);
drawArc(ctx, cent, angle_start, angle_end, [UIColor purpleColor],self.radius);
}
-(void)setRadius:(float)radius
{
_radius=radius;
[self setNeedsDisplay];
}
@end
pieViewController.m文件
@implementation pieViewController
- (void)viewDidLoad {
[super viewDidLoad];
pieview *view=[[pieview alloc]init];
view.radius=50;
view.frame=CGRectMake(4, 150, 150, 300);
[self.view addSubview:view];
//view.backgroundColor=[UIColor clearColor];
//模拟5秒后执行这个段代码
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
view.radius=20;
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果
5秒之后
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# ios开发中如何画扇形图
# IOS
# 扇形图
# 画扇形图
# iOS画出精美的图表方法示例
# iOS App开发中用CGContextRef绘制基本图形的基本示例
# iOS新增绘制圆的方法实例代码
# iOS使用Charts框架绘制饼状图
# iOS使用Charts框架绘制折线图
# IOS绘制虚线的方法总结
# iOS绘制专属于程序猿的浪漫爱心
# IOS绘制动画颜色渐变折线条
# iOS开发中使用Quartz2D绘图及自定义UIImageView控件
# iOS开发CGContextRef画图使用总结
# 好了
# 自定义
# 这是
# 子类
# 我就
# 在这里
# 我会
# 去了
# 让我们
# 就不
# 网上
# 我们可以
# 没办法
# 希望能
# 我做
# 看一下
# 谢谢大家
# 可使
# 提供一个
# 没有找到
相关文章:
北京网站制作网页,网站升级改版需要多久?
如何高效完成独享虚拟主机建站?
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
如何在景安云服务器上绑定域名并配置虚拟主机?
如何在Windows虚拟主机上快速搭建网站?
Swift中循环语句中的转移语句 break 和 continue
潮流网站制作头像软件下载,适合母子的网名有哪些?
网站制作公司排行榜,四大门户网站排名?
如何在阿里云虚拟服务器快速搭建网站?
如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本
如何快速生成橙子建站落地页链接?
外贸公司网站制作,外贸网站建设一般有哪些步骤?
建站主机选购指南:核心配置与性价比推荐解析
电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?
武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?
岳西云建站教程与模板下载_一站式快速建站系统操作指南
公司网站设计制作厂家,怎么创建自己的一个网站?
建站之星后台密码遗忘?如何快速找回?
ppt制作免费网站有哪些,ppt模板免费下载网站?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
如何确保西部建站助手FTP传输的安全性?
c# await 一个已经完成的Task会发生什么
高端网站建设与定制开发一站式解决方案 中企动力
如何选择香港主机高效搭建外贸独立站?
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
Python多线程使用规范_线程安全解析【教程】
如何通过智能用户系统一键生成高效建站方案?
网站插件制作软件免费下载,网页视频怎么下到本地插件?
如何通过FTP空间快速搭建安全高效网站?
网站制作模板下载什么软件,ppt模板免费下载网站?
如何在搬瓦工VPS快速搭建网站?
如何获取免费开源的自助建站系统源码?
如何快速打造个性化非模板自助建站?
网站图片在线制作软件,怎么在图片上做链接?
攀枝花网站建设,攀枝花营业执照网上怎么年审?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
香港服务器租用每月最低只需15元?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
html制作网站的步骤有哪些,iapp如何添加网页?
如何基于PHP生成高效IDC网络公司建站源码?
建站OpenVZ教程与优化策略:配置指南与性能提升
如何在服务器上配置二级域名建站?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
如何通过万网虚拟主机快速搭建网站?
如何在IIS7中新建站点?详细步骤解析
深圳网站制作的公司有哪些,dido官方网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。