iOS drawRect绘制圆形/圆环/饼图
效果如下图的圆,中间的内容可自己添加;隐藏中间白色的圆即是一个饼图
调用方法:
GradeView *gradeView = [[GradeView alloc] init];
gradeView.grade = @"0.8";
需要更新需要调用:
gradeView.grade = @"0.9";
[gradeView setNeedsDisplay];
GradeView.h
#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface GradeView : UIView@property (nonatomic, strong) NSString *grade;@endNS_ASSUME_NONNULL_END
GradeView.m
#import "GradeView.h"@implementation GradeView- (void)setGrade:(NSString *)grade {_grade = grade;
}//计算度转弧度
static inline float radians(double degrees) {return degrees * M_PI / 180;
}- (void)drawRect:(CGRect)rect {[super drawRect:rect];CGFloat borderWidth = 20;CGFloat radius = self.bounds.size.height / 2;CGFloat grade = self.grade ? self.grade.floatValue : 1;grade = grade > 0 ? grade : 0;UIColor *bgColor = HEXCOLOR(@"ffffff");UIColor *tintColor = Color_Global_Light;UIColor *onTintColor = Color_Global;CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);CGContextRef context = UIGraphicsGetCurrentContext();CGContextClearRect(context, rect);//填充当区域内的颜色与父视图相同[bgColor set];CGContextFillRect(context, rect);CGContextFillPath(context);//绘制两个扇形float angle_start = radians(0.0);float angle_end = radians(360 * grade);CGContextMoveToPoint(context, center.x, center.y);CGContextSetFillColor(context, CGColorGetComponents(onTintColor.CGColor));CGContextAddArc(context, center.x, center.y, radius, angle_start, angle_end, 0);CGContextFillPath(context);angle_start = angle_end;angle_end = radians(360.0);CGContextMoveToPoint(context, center.x, center.y);CGContextSetFillColor(context, CGColorGetComponents(tintColor.CGColor));CGContextAddArc(context, center.x, center.y, radius, angle_start, angle_end, 0);CGContextFillPath(context);//绘制圆颜色与父视图相同CGFloat w = (self.bounds.size.width)/ 2 - borderWidth;CGContextAddEllipseInRect(context, CGRectMake(center.x - w, center.y - w, w * 2, w * 2));[bgColor set];CGContextFillPath(context);//实心的CGContextStrokePath(context);//空心的//调整方向self.layer.transform = CATransform3DMakeRotation(M_PI * -0.5, 0, 0, 1);self.layer.shadowRadius = radius;self.layer.masksToBounds = YES;
}@end
iOS drawRect绘制圆形/圆环/饼图相关推荐
- html5 canvas 圆圈,使用HTML5 Canvas arc()绘制圆形/圆环
前段时间的一个项目做到这个玩意,研究了一下绘制圆环的方法.最终使用HTML5的画布标签来做.arc()是HTML5 Canvas的一个API函数,作用是"创建弧/曲线(用于创建圆形或部分圆) ...
- 绘制圆形/圆环/扇形/扇面Mesh
using UnityEngine; using System.Collections;[RequireComponent(typeof(MeshRenderer), typeof(MeshFilte ...
- quartz2D 如何绘制圆形图片, 及圆环图片
转载自:http://www.cnblogs.com/yuwei0911/p/5277886.html -(void)touchesBegan:(NSSet<UITouch *> *)to ...
- matplotlib绘制直方图,饼图,散点图,气泡图,箱型图,雷达图
matplotlib绘制直方图,饼图,散点图,气泡图,箱型图,雷达图 一.直方图 用10000个正态分布随机数画直方图 二.绘制饼图或者圆环图 圆环图 根据消费支出画圆环图 三.绘制散点图或气泡图 使 ...
- android绘制环形进度_Android使用Canvas绘制圆形进度条效果
前言 Android自定义控件经常会用到Canvas绘制2D图形,在优化自己自定义控件技能之前,必须熟练掌握Canvas绘图机制.本文从以下三个方面对Canvas绘图机制进行讲解: 画布Canvas ...
- qt绘制一圈圆_Qt绘制圆形,矩形等图形 绘制同心圆
原来Qt也可以绘制图形啊. 利用QPainter类来绘制,再辅以其他类,诸如QPen来说明绘制的画笔,QPoint来说明某个点,QPaletee来说明绘制的颜料等等.在QWidget类中来实现虚拟函数 ...
- 精通Android自定义View(十二)绘制圆形进度条
1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...
- 在iOS上绘制自然的签名
在iOS上绘制自然的签名 这里有一篇很棒的文章写如何在Android上获取流畅的签名:Smoother Signatures,但是我没有找到一篇是写在iOS上如何实现.那么,究竟怎么做才能在iOS设备 ...
- Matplotlib plt.polar绘制圆形
代码如下: # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as pltplt.axes(projection=' ...
最新文章
- 尼克的任务 dp 洛谷1280
- 【C#串口编程计划】如何避免关闭串口时软件死锁
- pandas KeyError [‘1‘] not found in axis 错误的解决方法
- python3 发送邮件
- python 类继承和组合_Python:继承与组合
- abap 调用外部web service 使用小结
- MTK 驱动(83)---mtklog相关知识
- paip.信用卡账单处理系统功能vO22
- http://95u.free.fr/index.php,Electronic Software Distribution Service
- DLL注入之修改PE静态注入
- 苍井空老师的Twitter
- react在线编辑器
- 身份证识别鉴别证件真假
- excel曲线拟合怎么弄
- JAVA 三种方式计算1加到100的结果
- .NET的.snk文件使用方法
- virtualbox报错 提示:E_FAIL (0x80004005)
- java多线程之Guarded Suspension模式
- 云计算基础之如何学习云计算?
- TreeView 控件