生活随笔
收集整理的這篇文章主要介紹了
IOS开发-画饼图画柱状图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
餅圖
(1)自定義一個view
(2)導入QuartzCore框架
(3)m文件代碼如下:
#import "ZCView.h"
#import <QuartzCore/QuartzCore.h>
#define ARC4RANDOM_MAX 0x100000000
#define ZCColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define ZCRandomColor ZCColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@interface ZCView ()
@property(
nonatomic,
weak)
UILabel *progressLabel;
@end@implementation ZCView- (
NSArray *)getPercentArray
{
float total =
100.0;
int count = arc4random_uniform(
15)+
1;
NSMutableArray *tempArray = [
NSMutableArray array];
float temp =
0;
for (
int i =
0; i<count; i++) {temp = floorf(((
double)arc4random() / ARC4RANDOM_MAX) * total);[tempArray addObject:@(temp)];total -= temp;}
if(total){[tempArray addObject:@(total)];}
return tempArray;
}- (
void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{[
self setNeedsDisplay];
}- (
void)drawRect:(
CGRect)rect {
CGFloat lineWidth =
5;
CGFloat radius = (rect
.size.width-lineWidth*
4)/
2;
CGPoint center = CGPointMake(rect
.size.width/
2, rect
.size.width/
2);
NSArray *percentArray = [
self getPercentArray];
CGFloat startAngle =
0;
CGFloat angle =
0;
CGFloat endAngle =
0;
for (
int i =
0; i<percentArray
.count; i++) {startAngle = endAngle;angle = [percentArray[i] floatValue] /
100.0 * M_PI *
2;endAngle = startAngle + angle;UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:
YES];[path addLineToPoint:center];[ZCRandomColor set];[path fill];}
}
效果如下(點擊自定義view會更換比例):
柱狀圖
(1)自定義一個view
(2)導入QuartzCore框架
(3)m文件代碼如下:
#import "ZCView.h"
#import <QuartzCore/QuartzCore.h>
#define ARC4RANDOM_MAX 0x100000000
#define ZCColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define ZCRandomColor ZCColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))@interface ZCView ()
@property(
nonatomic,
weak)
UILabel *progressLabel;
@end@implementation ZCView
- (
NSArray *)getPercentArray
{
float total =
100.0;
int count = arc4random_uniform(
6)+
1;
NSMutableArray *tempArray = [
NSMutableArray array];
float temp =
0;
for (
int i =
0; i<count; i++) {temp = floorf(((
double)arc4random() / ARC4RANDOM_MAX) * total);[tempArray addObject:@(temp)];total -= temp;}
if(total){[tempArray addObject:@(total)];}
return tempArray;
}
- (
void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{[
self setNeedsDisplay];
}
- (
void)drawRect:(
CGRect)rect
{
NSArray *percentArray = [
self getPercentArray];
CGFloat X =
0;
CGFloat Y =
0;
CGFloat W =
0;
CGFloat H =
0;
for (
int i =
0; i < percentArray
.count; i++) {W = rect
.size.width / (
2*percentArray
.count-
1);X =
2 * W * i;H = [percentArray[i] floatValue] /
100.0 * rect
.size.height;Y = rect
.size.height - H;UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(X, Y, W, H)];[ZCRandomColor set];[path fill];}
}
效果如下,點擊自定義view可以切換比例:
這里介紹的是簡易的畫圖,復雜點的網上有第三方的框架可供使用。
總結
以上是生活随笔為你收集整理的IOS开发-画饼图画柱状图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。