效果:

#import <UIKit/UIKit.h>

@interface HsProfitRatePieWidgets : UIView
{
UILabel *_textLabel;
} @property (nonatomic) double progress; @property (nonatomic) NSInteger showText UI_APPEARANCE_SELECTOR;
@property (nonatomic) NSInteger roundedHead UI_APPEARANCE_SELECTOR;
@property (nonatomic) NSInteger showShadow UI_APPEARANCE_SELECTOR; @property (nonatomic) CGFloat thicknessRatio UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *innerBackgroundColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *outerBackgroundColor UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIFont *font UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *progressFillColor UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *progressTopGradientColor UI_APPEARANCE_SELECTOR;
@property (nonatomic, strong) UIColor *progressBottomGradientColor UI_APPEARANCE_SELECTOR; @end

.m实现文件中

#import "HsProfitRatePieWidgets.h"

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

@implementation HsProfitRatePieWidgets

- (void)dealloc
{
[_textLabel release];
[super dealloc];
} + (void)initialize
{
if (self == [HsProfitRatePieWidgets class])
{
id appearance = [self appearance]; [appearance setShowText:YES];//用来控制是否显示显示label
[appearance setRoundedHead:YES];//用来控制是否对进度两边进行处理
[appearance setShowShadow:YES];//控制圆环进度条的样式 [appearance setThicknessRatio:0.37f];//圆环进度条的宽度 [appearance setInnerBackgroundColor:nil];
[appearance setOuterBackgroundColor:nil]; [appearance setProgressFillColor:[UIColor redColor]];//整个进度条颜色
[appearance setProgressTopGradientColor:[UIColor greenColor]];//进度条前半部分颜色
[appearance setProgressBottomGradientColor:[UIColor redColor]];//进度条后半部分颜色
[appearance setBackgroundColor:[UIColor clearColor]];
}
} - (id)init
{
self = [super initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]; return self;
} #pragma mark - Drawing - (void)drawRect:(CGRect)rect
{
// Calculate position of the circle
CGFloat progressAngle = _progress * 360.0 - ;
CGPoint center = CGPointMake(rect.size.width / 2.0f, rect.size.height / 2.0f);
CGFloat radius = MIN(rect.size.width, rect.size.height) / 2.0f; CGRect square;
if (rect.size.width > rect.size.height)
{
square = CGRectMake((rect.size.width - rect.size.height) / 2.0, 0.0, rect.size.height, rect.size.height);
}
else
{
square = CGRectMake(0.0, (rect.size.height - rect.size.width) / 2.0, rect.size.width, rect.size.width);
} //进度条宽度
CGFloat circleWidth = radius * _thicknessRatio;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context); if (_innerBackgroundColor)
{
//内环
// Fill innerCircle with innerBackgroundColor
UIBezierPath *innerCircle = [UIBezierPath bezierPathWithArcCenter:center
radius:radius - circleWidth
startAngle:*M_PI
endAngle:0.0
clockwise:YES]; [_innerBackgroundColor setFill]; [innerCircle fill];
} if (_outerBackgroundColor)
{
//外环
// Fill outerCircle with outerBackgroundColor
UIBezierPath *outerCircle = [UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:0.0
endAngle:*M_PI
clockwise:NO];
[outerCircle addArcWithCenter:center
radius:radius - circleWidth
startAngle:*M_PI
endAngle:0.0
clockwise:YES]; [_outerBackgroundColor setFill]; [outerCircle fill];
} if (_showShadow)
{
//圆环背景处理
CGFloat locations[] = { 0.0f, 0.33f, 0.66f, 1.0f };
// NSArray *gradientColors = @[
// (id)[[UIColor colorWithWhite:0.3 alpha:0.5] CGColor],
// (id)[[UIColor colorWithWhite:0.9 alpha:0.0] CGColor],
// (id)[[UIColor colorWithWhite:0.9 alpha:0.0] CGColor],
// (id)[[UIColor colorWithWhite:0.3 alpha:0.5] CGColor],
// ]; NSArray *gradientColors = @[
(id)[[UIColor colorWithWhite:0.7 alpha:] CGColor],
(id)[[UIColor colorWithWhite:0.7 alpha:] CGColor],
(id)[[UIColor colorWithWhite:0.7 alpha:] CGColor],
(id)[[UIColor colorWithWhite:0.7 alpha:] CGColor],
]; CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)gradientColors, locations);
CGContextDrawRadialGradient(context, gradient, center, radius - circleWidth, center, radius, );
CGGradientRelease(gradient);
CGColorSpaceRelease(rgb);
} if (_showText)
{
if (!_progress) {
return;
}
if ([self viewWithTag:]) {
[_textLabel removeFromSuperview];
}
//中间显示label
_textLabel = [[[UILabel alloc] init] autorelease];
//字符串处理
NSString *str = [NSString stringWithFormat:@"%0.2f%%\n收益率",_progress * 100.0];
NSMutableAttributedString *progressStr = [[NSMutableAttributedString alloc] initWithString:str];
//前6位字符 颜色
[progressStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(, )];
//第七位向后的3个字符 颜色
[progressStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(, )];
[progressStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16.0] range:NSMakeRange(, )];
[progressStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:] range:NSMakeRange(, )];
_textLabel.attributedText = progressStr;
_textLabel.numberOfLines = ;
_textLabel.bounds = CGRectMake(, , , );
_textLabel.center = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
_textLabel.tag = ;
_textLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_textLabel]; } UIBezierPath *path = [UIBezierPath bezierPath]; [path appendPath:[UIBezierPath bezierPathWithArcCenter:center
radius:radius
startAngle:DEGREES_TO_RADIANS(-)
endAngle:DEGREES_TO_RADIANS(progressAngle)
clockwise:YES]]; if (_roundedHead)
{
//终点处理
CGPoint point;
point.x = (cos(DEGREES_TO_RADIANS(progressAngle)) * (radius - circleWidth/)) + center.x;
point.y = (sin(DEGREES_TO_RADIANS(progressAngle)) * (radius - circleWidth/)) + center.y; [path addArcWithCenter:point
radius:circleWidth/
startAngle:DEGREES_TO_RADIANS(progressAngle)
endAngle:DEGREES_TO_RADIANS(270.0 + progressAngle - 90.0)
clockwise:YES];
} [path addArcWithCenter:center
radius:radius-circleWidth
startAngle:DEGREES_TO_RADIANS(progressAngle)
endAngle:DEGREES_TO_RADIANS(-)
clockwise:NO];
//起始点添加分割线
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(radius, , , circleWidth)];
lineView.backgroundColor = [UIColor whiteColor];
[self addSubview:lineView]; if (_roundedHead)
{
//起始点处理
// CGPoint point;
// point.x = (cos(DEGREES_TO_RADIANS(-90)) * (radius - circleWidth/2)) + center.x;
// point.y = (sin(DEGREES_TO_RADIANS(-90)) * (radius - circleWidth/2)) + center.y;
//
// [path addArcWithCenter:point
// radius:circleWidth/2
// startAngle:DEGREES_TO_RADIANS(90)
// endAngle:DEGREES_TO_RADIANS(-90)
// clockwise:NO];
} [path closePath];
//进度条颜色处理
if (_progressFillColor)
{
[_progressFillColor setFill];
[path fill];
}
else if (_progressTopGradientColor && _progressBottomGradientColor)
{
[path addClip]; NSArray *backgroundColors = @[
(id)[_progressTopGradientColor CGColor],
(id)[_progressBottomGradientColor CGColor],
];
CGFloat backgroudColorLocations[] = { 0.0f, 1.0f };
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGGradientRef backgroundGradient = CGGradientCreateWithColors(rgb, (__bridge CFArrayRef)(backgroundColors), backgroudColorLocations);
CGContextDrawLinearGradient(context,
backgroundGradient,
CGPointMake(0.0f, square.origin.y),
CGPointMake(0.0f, square.size.height),
);
CGGradientRelease(backgroundGradient);
CGColorSpaceRelease(rgb);
} CGContextRestoreGState(context);
} #pragma mark - Setter
//设置进度
- (void)setProgress:(double)progress
{
_progress = MIN(1.0, MAX(0.0, progress)); [self setNeedsDisplay];
} #pragma mark - UIAppearance - (void)setShowText:(NSInteger)showText
{
_showText = showText; [self setNeedsDisplay];
} - (void)setRoundedHead:(NSInteger)roundedHead
{
_roundedHead = roundedHead; [self setNeedsDisplay];
} - (void)setShowShadow:(NSInteger)showShadow
{
_showShadow = showShadow; [self setNeedsDisplay];
}
//进度条宽度与半径之比
- (void)setThicknessRatio:(CGFloat)thickness
{
_thicknessRatio = MIN(MAX(0.0f, thickness), 1.0f); [self setNeedsDisplay];
}
//内环
- (void)setInnerBackgroundColor:(UIColor *)innerBackgroundColor
{
_innerBackgroundColor = innerBackgroundColor; [self setNeedsDisplay];
}
//外环
- (void)setOuterBackgroundColor:(UIColor *)outerBackgroundColor
{
_outerBackgroundColor = outerBackgroundColor; [self setNeedsDisplay];
}
//进度条颜色
- (void)setProgressFillColor:(UIColor *)progressFillColor
{
_progressFillColor = progressFillColor; [self setNeedsDisplay];
}
//进度条前半部分颜色
- (void)setProgressTopGradientColor:(UIColor *)progressTopGradientColor
{
_progressTopGradientColor = progressTopGradientColor; [self setNeedsDisplay];
}
//进度条后半部分颜色
- (void)setProgressBottomGradientColor:(UIColor *)progressBottomGradientColor
{
_progressBottomGradientColor = progressBottomGradientColor; [self setNeedsDisplay];
} @end

展现:

- (void)viewDidLoad {
[super viewDidLoad];
HsProfitRatePieWidgets *circleProgress = [[HsProfitRatePieWidgets alloc] initWithFrame:CGRectMake(, , , )];
circleProgress.progress = 0.432;
[self.view addSubview:circleProgress];
[circleProgress release];
}

iOS学习-圆形进度条的更多相关文章

  1. xamarin.ios 实现圆形进度条

    using System; using UIKit; using System.Drawing; using CoreAnimation; namespace PMM { public class P ...

  2. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. [iOS]圆形进度条及计时功能

    平时用战网安全令的时候很喜欢圆形倒计时的效果,然后简单看了一下Android的圆形进度条,后来又写了一个IOS的.整体界面参照IOS系统的倒计时功能,顺便熟悉了UIPickerView的一些特性的实现 ...

  4. IOS 圆形进度条

    // // CCProgressView.h // Demo // // Created by leao on 2017/8/7. // Copyright © 2017年 zaodao. All r ...

  5. iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现

    最近同样是新App,设计稿里出现一种圆形进度条的设计,如下: 想了想,圆形进度条实现起来不难,但是其中显示百分比的文本确需要自适应,虽然可以使用时自己设定文本字体的大小,但是这样显得很麻烦,也很low ...

  6. 利用css3动画和border来实现圆形进度条

    最近在学习前端的一些知识,发现border的功能十分强大啊! 首先来看看demo 就是这么一个圆形的进度条,在文本框中输入0-100的数值下面的进度条相应的转到多少 这个主要是利用border,旋转和 ...

  7. WPF利用动画实现圆形进度条

    原文:WPF利用动画实现圆形进度条 这是我的第一篇随笔,最近因为工作需要,开始学习WPF相关技术,自己想实现以下圆形进度条的效果,逛了园子发现基本都是很久以前的文章,实现方式一般都是GDI实现的,想到 ...

  8. COCOS2D-X之圆形进度条的一个简单Demo

    这应该是游戏中很常见的一个效果.显示某个事件的进度等,在加载资源或者联网的时候经常用到.所以有必要学习学习 一.我们直接在COCOS2D-X自带的HelloCpp的工程中添加代码即可.我们在初始化中添 ...

  9. Android 高手进阶,自己定义圆形进度条

    背景介绍 在Android 开发中,我们常常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,许多时候须要我们自定义控件,在开发的过程中.我们公司遇到了一种须要自己写的一 ...

随机推荐

  1. C#调用SendMessage 用法

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一.    函数原型:LRESUL ...

  2. ubuntu学习的简单笔记

    l vi编辑器开发步骤 A)输入 vi Hello.java B) 输入 i 插入模式. C)输入 冒号.[保存退出:wq][退出不保存:q!] l 列出当前目录的所有文件:ls 详细信息的列表:ls ...

  3. python之最强王者(4)——字符串

    1.Python 中文编码 前面章节中我们已经学会了如何用 Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界" ...

  4. 通过JAXB完成Java对象与XML之间的转换

    Java对象转换XML的过程叫marshal. XML转换到Java对象的过程叫unmarshal. 一.Java对象转化为XML 这里省略getter和setter方法 通过标注@XMLRootEl ...

  5. Struts2入门(四)——数据输入验证

    一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...

  6. 关于Karaf Container 4.0.7

            Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署各种组件,应用程序.Karaf提供了很多 ...

  7. 学习廖雪峰的git教程

    地址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 1.git add:添加文件 ...

  8. js url.slice(star,end) url.lastIndexOf('/') + 1, -4

    var url = '"http://60.195.252.25:15518/20151228/XXSX/作三角形的高.mp4")' document.title = url.sl ...

  9. Sharepoint 2010、Sharepoint 2013浏览器打开CAD(.dwg)

    客户端配置 1.安装FreeDWGViewer.exe,设置浏览器查看 2.检查ActiveX插件是否已安装成功 服务端配置 1.开启许可模式或者通过脚本将"application/acad ...

  10. Android 手机卫士--导航界面3、4和功能列表界面跳转逻辑处理

    刚刚花了一点时间,将导航界面3.4的布局和相应的跳转逻辑写了一下: Setup3Activity代码如下: /** * Created by wuyudong on 2016/10/10. */ pu ...