IOS开发基础之绘制饼图、柱状图、自定义进度条

源码在我的主页里
1.绘制饼图
效果

源码

//  LJView.m
//  34-绘图饼图
//  Created by 鲁军 on 2021/2/23.
#import "LJView.h"@implementation LJView
- (void)drawRect:(CGRect)rect {NSArray *array = @[@0.3,@0.1,@0.2,@0.4];CGFloat end=0,start = 0;for(int i=0;i<array.count;++i){end =2*M_PI* [array[i] floatValue] + start;UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:start endAngle:end clockwise:1];[path addLineToPoint:CGPointMake(150, 150)];//随机的颜色[[UIColor colorWithRed:((float)arc4random_uniform(256)/255.0) green:((float)arc4random_uniform(256)/255.0)  blue:((float)arc4random_uniform(256)/255.0)  alpha:1.0] set];[path fill];//下一次的起点等于上一次的终点start = end;}
}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{// NSLog(@"123");//  [self setNeedsDisplay];//刷新指定的区域[self setNeedsDisplayInRect:CGRectMake(0, 0, 150, 150)];
}
@end

2.绘制柱状图

//
//  LJView.m
//  35-柱状图绘图
//
//  Created by 鲁军 on 2021/2/23.
//#import "LJView.h"@implementation LJView- (void)drawRect:(CGRect)rect {NSArray *array = @[@1,@0.5,@0.7,@0.3,@0.1,@0.6];for(int i=0;i<array.count;++i){CGFloat w =20 ,h =[array[i] floatValue] * rect.size.height ,x =i*2*w,y=rect.size.height-h;UIBezierPath *path  =[UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];//随机的颜色[[UIColor colorWithRed:((float)arc4random_uniform(256)/255.0) green:((float)arc4random_uniform(256)/255.0)  blue:((float)arc4random_uniform(256)/255.0)  alpha:1.0] set];[path fill];
}}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{[self setNeedsDisplay];}@end

3.绘制自定义进度条

#import <UIKit/UIKit.h>
@interface LJView : UIView
@property(nonatomic,assign)CGFloat progressValue;
@end
//
//  LJView.m
//  35-自定义进度条
//
//  Created by 鲁军 on 2021/2/23.
//#import "LJView.h"@interface LJView ()@property (weak, nonatomic) IBOutlet UILabel *progressLbl;@end@implementation LJView- (void)setProgressValue:(CGFloat)progressValue{_progressValue = progressValue;self.progressLbl.text = [NSString stringWithFormat:@"%.2f%%",progressValue *100];[self setNeedsDisplay];}- (void)drawRect:(CGRect)rect {UIBezierPath *path  = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0-M_PI_2 endAngle:2*M_PI *self.progressValue - M_PI_2 clockwise:1];[path addLineToPoint:CGPointMake(150, 150)];[[UIColor redColor] setFill];[path fill];}@end
//
//  ViewController.m
//  35-自定义进度条
//
//  Created by 鲁军 on 2021/2/23.
//#import "ViewController.h"
#import "LJView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet LJView *progressView;
//@end@implementation ViewController
- (IBAction)sender:(UISlider *)sender {self.progressView.progressValue = sender.value;}
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.
}
@end

//  LJView.m
//  33-绘图的样式
//  Created by 鲁军 on 2021/2/23.
#import "LJView.h"
@implementation LJView
- (void)drawRect:(CGRect)rect {//    [self testCStyle];
//    [self testOCStyleOCcode];[self testJiOu];}
//使用奇偶填充规则
-(void)testJiOu{UIBezierPath *path =[UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 100)];[path addArcWithCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:1];path.usesEvenOddFillRule =YES;[path fill];}-(void)testSanjiaoxingOC{UIBezierPath *path = [UIBezierPath bezierPath];[path moveToPoint:CGPointMake(50, 50)];[path addLineToPoint:CGPointMake(100, 100)];[path addLineToPoint:CGPointMake(150, 50)];[path closePath];[path setLineWidth:30];[[UIColor redColor]setFill];[[UIColor blueColor] setStroke];//同时设置描边的和填充的颜色[[UIColor greenColor] set];[path stroke];[path fill];}
-(void)testSanjiaoxingC{CGContextRef ctx =UIGraphicsGetCurrentContext();CGContextMoveToPoint(ctx, 50, 50);CGContextAddLineToPoint(ctx, 100, 100);CGContextAddLineToPoint(ctx, 150, 50);
//    CGContextAddLineToPoint(ctx, 50, 50);CGContextClosePath(ctx);  //关闭路径CGContextSetLineWidth(ctx, 10);
//    CGContextStrokePath(ctx); //描边//填充
//    CGContextFillPath(ctx);//CGContextDrawPath(ctx, kCGPathStroke);[[UIColor redColor] setFill];[[UIColor blueColor] setStroke];CGContextDrawPath(ctx, kCGPathFillStroke);
}-(void)testOCStyleOCcode{UIBezierPath *path =[UIBezierPath bezierPath];[path moveToPoint:CGPointMake(50, 50)];[path addLineToPoint:CGPointMake(100, 100)];[path addLineToPoint:CGPointMake(150, 50)];//设置线宽[path setLineWidth:30];//设置连接处样式[path setLineJoinStyle:kCGLineJoinRound];//设置头尾样式[path setLineCapStyle:kCGLineCapRound];//OC 设置蓝色[[UIColor blueColor] setStroke];[path stroke];
}-(void)testCStyle{CGContextRef ctx = UIGraphicsGetCurrentContext();CGContextMoveToPoint(ctx, 50, 50);CGContextAddLineToPoint(ctx, 100, 100);CGContextAddLineToPoint(ctx, 150, 50);//设置线宽CGContextSetLineWidth(ctx, 30);//连接处的样式//kCGLineJoinMiter 默认//kCGLineJoinBevel  切角//kCGLineJoinRound。圆角CGContextSetLineJoin(ctx, kCGLineJoinRound);//头尾样式//kCGLineCapButt。默认//kCGLineCapRound。圆角//kCGLineCapSquare。方的。变短一点。CGContextSetLineCap(ctx, kCGLineCapSquare);//C 设置红色CGContextSetRGBStrokeColor(ctx,1, 0, 0, 1);CGContextStrokePath(ctx);
}
@end

IOS开发基础之绘制饼图、柱状图、自定义进度条相关推荐

  1. iOS开发基础知识--碎片44

    iOS开发基础知识--碎片44  iOS开发基础知识--碎片44 1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = ...

  2. ios开发基础之通讯录系统实战-20

    ios开发基础之通讯录系统实战 基础知识 OC 基础 segue 的使用.delegate 代理的使用 自定义代理.面向对象思想 沙盒容器的数据持久化方案, controller 之间的跳转 ,登录方 ...

  3. IOS开发基础之微博项目

    IOS开发基础之微博项目 关键性代码 // // NJViewController.m // 06-预习-微博(通过代码自定义cell)// #import "NJViewControlle ...

  4. iOS开发基础知识--碎片27

     iOS开发基础知识--碎片27 1:iOS中的round/ceil/floorf extern float ceilf(float); extern double ceil(double); ext ...

  5. iOS开发基础知识--碎片41

    iOS开发基础知识--碎片41 1:UIWebView加载本地的HTML NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *bas ...

  6. iOS开发基础知识--碎片19

    iOS开发基础知识--碎片19  1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // ...

  7. iOS开发基础-九宫格坐标(4)

    对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...

  8. QT绘制饼图和自定义饼图切片

    QT绘制饼图和自定义饼图切片 项目简介 项目技术 项目展示 主要源码片段解析 获取完整项目源码传送门 项目简介 创建一个简单的饼图以及如何对饼图切片进行一些自定义. 项目技术 qt5.12,qt ch ...

  9. IOS开发基础之OC的Block入门_Day09-Block

    IOS开发基础之OC的Block入门_Day09-Block block是oc的重要的基础知识,重点之重.跟协议一样重要,是进行函数回调重要手段.在后续的UI学习具有举足轻重的地位.学会基础的bloc ...

最新文章

  1. PostgreSQL11.3 创建用户和创建数据库
  2. 二十四、死锁的处理策略---检测和解除
  3. freemarker include 和 import
  4. 关闭 进程_关闭一个进程 锐龙CPU骤然降温22.5!
  5. Android工程中R.java文件的重新生成——注意资源文件的错误
  6. 虚拟计算机组成,计算机组成原理虚拟仿真实验平台研究
  7. Linux基础之文件权限详解
  8. 01_配置管理和SaltStack概述
  9. IM系统中聊天记录模块的设计与实现
  10. ubuntu 安装php gd,如何在ubuntu上安装php5-gd?
  11. C/C++[sort( )]排序
  12. Kali渗透测试:使用Word宏病毒进行渗透攻击
  13. excel转换linux时间戳,在Excel中转换时间戳(timeStamp)
  14. PT px pc 的区别
  15. openSUSE Tumbleweed 连接 Canon LBP2900 打印机
  16. 网站添加百度分享工具的好处
  17. python读取plt文件吗_如何读取连续的.plt文件并存储它们
  18. 我的达内Java培训经历
  19. 安卓11添加第三方输入法替换默认输入法
  20. Linux 常用命令大全(虚拟机)

热门文章

  1. Java十大排序算法总结,Java排序算法总结之冒泡排序
  2. 软件测试师具备的素质_软件测试工程师有哪些需要具备的能力呢
  3. case when mysql_mysql条件语句case when的实例
  4. linux打开图形化命令,在Linux命令行中以图形化窗口打开文件夹
  5. 送30块树莓派PICO 开发板!
  6. 21年编程,那些我踩过的坑!
  7. 单片机开发设计之十层功力
  8. PCB上晶振布局是个技术活,避开PCB的边缘很重要
  9. 我来自双非二本,未来的路该怎么走?
  10. LL-verilog语法多位宽全加器