IOS开发-画饼图画柱状图

本文介绍如何使用Objective-C自定义UIView绘制饼图和柱状图,并通过随机分配百分比实现动态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

饼图

(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
{
    //总百分比是100
    float total = 100.0;
    //要把饼图分成count份
    int count = arc4random_uniform(15)+1;
    //记录的数组
    NSMutableArray *tempArray = [NSMutableArray array];
    //临时存值
    float temp = 0;

    for (int i = 0; i<count; i++) {
        //使用arc4random() 来获取0到100之间浮点数了(精度是rand()的两倍),代码如下:
        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
{
    //总百分比是100
    float total = 100.0;
    //要把饼图分成count份
    int count = arc4random_uniform(6)+1;
    //记录的数组
    NSMutableArray *tempArray = [NSMutableArray array];
    //临时存值
    float temp = 0;

    for (int i = 0; i<count; i++) {
        //使用arc4random() 来获取0到100之间浮点数了(精度是rand()的两倍),代码如下:
        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];
    //矩形frame
    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可以切换比例:
这里写图片描述

这里介绍的是简易的画图,复杂点的网上有第三方的框架可供使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值