zoukankan      html  css  js  c++  java
  • iOS-饼图

    //
    //  UIColor+Random.h
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface UIColor (Random)
    + (UIColor *)randomColor;
    @end
    
    
    
    //
    //  UIColor+Random.m
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "UIColor+Random.h"
    
    @implementation UIColor (Random)
    + (UIColor *)randomColor
    {
        CGFloat r = arc4random_uniform(256)/255.0;
        CGFloat g = arc4random_uniform(256)/255.0;
        CGFloat b = arc4random_uniform(256)/255.0;
    
        return [UIColor colorWithRed:r green:g blue:b alpha:1];
    }
    @end
    //
    //  HMPieView.h
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface HMPieView : UIView
    
    @end
    
    
    
    //
    //  HMPieView.m
    //  饼图
    //
    //  Created by YaguangZhu on 15/9/9.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "HMPieView.h"
    #import "UIColor+Random.h"
    
    @implementation HMPieView
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
        NSArray *data = @[@25,@25,@50];
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGPoint center = CGPointMake(125, 125);
        CGFloat radius = 120;
        CGFloat startA = 0;
        CGFloat angle = 0;
        CGFloat endA = 0;
        for (NSNumber *number in data) {
            startA = endA;
            angle = number.intValue /100.0*M_PI*2;
            endA = startA +angle;
            UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
            [path1 addLineToPoint:center];
            CGContextAddPath(ctx, path1.CGPath);
            [[UIColor randomColor]set];
            CGContextFillPath(ctx);
        };
        
        
       
        
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self setNeedsDisplay];
    }
    
    - (void)drawPie
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGPoint center = CGPointMake(125, 125);
        CGFloat radius = 120;
        CGFloat startA = 0;
        CGFloat angle = 0;
        CGFloat endA = 0;
        
        angle = 25 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path addLineToPoint:center];
        CGContextAddPath(ctx, path.CGPath);
        [[UIColor redColor]set];
        CGContextFillPath(ctx);
        
        
        startA = endA;
        angle = 25 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path1 addLineToPoint:center];
        CGContextAddPath(ctx, path1.CGPath);
        [[UIColor greenColor]set];
        
        CGContextFillPath(ctx);
        
        startA = endA;
        angle = 50 /100.0*M_PI*2;
        endA = startA +angle;
        UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
        [path2 addLineToPoint:center];
        CGContextAddPath(ctx, path2.CGPath);
        [[UIColor blueColor]set];
        
        CGContextFillPath(ctx);
    }
    @end
  • 相关阅读:
    SQL反模式学习笔记16 使用随机数排序
    SQL反模式学习笔记21 SQL注入
    SQL反模式学习笔记22 伪键洁癖,整理数据
    SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题
    SQL反模式学习笔记19 使用*号,隐式的列
    SQL反模式学习笔记17 全文搜索
    SQL反模式学习笔记20 明文密码
    (可发送)亿级流量APP,是怎么做前端性能测试自动化的?
    测试窝 高薪测试必备技能和 20+ 项目实战精华,好书免费领(限前 1000 名)!
    同样是断言,为何 Hamcrest 如此优秀?测试灵魂三问,该如何回答?
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4795630.html
Copyright © 2011-2022 走看看