//
// CCProgressView.h
// Demo
//
// Created by leao on 2017/8/7.
// Copyright © 2017年 zaodao. All rights reserved.
// #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, CCProgressViewStyle) {
CCProgressViewStyleCircle, // 圆形进度条
CCProgressViewStyleBar, // 条形进度条
CCProgressViewStyleDefault = CCProgressViewStyleCircle,
}; @interface CCProgressView : UIView
@property(nonatomic, assign, setter=setProgress:) CGFloat progress; // 0.0 ~ 1.0 @property(nonatomic, assign) CCProgressViewStyle progressViewStyle; // 进度条style
@property(nonatomic, strong) UIColor *trackTintColor; // 进度条背景色
@property(nonatomic, strong) UIColor *progressTintColor; // 进度条颜色
@property(nonatomic, strong) UIColor *progressFullTintColor; // 进度完成时progressTint的颜色
@property(nonatomic, assign) CGFloat lineWidth; // 绘制progress宽度 default: 10
@property(nonatomic, assign) CGFloat trackerWidth; // 绘制progress宽度 default: 10 // CCProgressViewStyleCircle 有效
@property(nonatomic, strong) UIColor *fillColor; // 中心颜色
@property(nonatomic, assign) BOOL clockwise; // 是否是顺时针 default: YES
@property(nonatomic, assign) CGFloat startAngle; // 进度条开始angle, default: -M_PI/2.0
@property (nonatomic, strong) UIButton *centerBtn; // 记录进度的Label
@property (nonatomic, strong) UIColor *labelbackgroundColor; // Label的背景色 默认clearColor
@property (nonatomic, strong) UIColor *textColor; // Label的字体颜色 默认黑色
@property (nonatomic, strong) UIFont *textFont; // Label的字体大小 默认15 - (void)setProgress:(CGFloat)progress;
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated;
@end
//
// CCProgressView.m
// Demo
//
// Created by leao on 2017/8/7.
// Copyright © 2017年 zaodao. All rights reserved.
// #import "CCProgressView.h"
#import <pop/POP.h>
#import <objc/runtime.h>
#import <ReactiveCocoa/ReactiveCocoa.h> #define kCCProgressFillColor [UIColor clearColor]
#define kCCProgressTintColor RGBCOLOR(214, 88, 45)
#define kCCTrackTintColor RGBCOLOR(243, 212, 187)
#define PROGRESS_WIDTH self.frame.size.width
#define PROGRESS_HEIGHT self.frame.size.height #define kAnimTimeInterval 2 @interface CCProgressView () @property(nonatomic, strong) CAShapeLayer *trackLayer;
@property(nonatomic, strong) CAShapeLayer *progressLayer; @end
@implementation CCProgressView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
} - (instancetype)init
{
self = [super init];
if (self) {
[self initSubviews];
}
return self;
} #pragma mark - private
- (void)initSubviews
{
_progressViewStyle = CCProgressViewStyleDefault;
_progressTintColor = kCCProgressTintColor;
_trackTintColor = kCCTrackTintColor;
_lineWidth = ;
_trackerWidth = ; _fillColor = kCCProgressFillColor;
_clockwise = YES;
_startAngle = - M_PI / 2.0; self.backgroundColor = [UIColor clearColor]; self.trackLayer = [CAShapeLayer layer];
self.trackLayer.lineCap = kCALineCapButt;
self.trackLayer.lineJoin = kCALineCapButt;
self.trackLayer.lineWidth = _lineWidth;
self.trackLayer.fillColor = nil;
self.trackLayer.strokeColor = _trackTintColor.CGColor;
self.trackLayer.frame = self.bounds;
[self.layer addSublayer:self.trackLayer]; self.progressLayer = [CAShapeLayer layer];
self.progressLayer.lineCap = kCALineCapButt;
self.progressLayer.lineJoin = kCALineCapButt;
self.progressLayer.lineWidth = _trackerWidth;
self.progressLayer.fillColor = _fillColor.CGColor;
self.progressLayer.strokeColor = _progressTintColor.CGColor;
self.progressLayer.frame = self.bounds;
[self.layer addSublayer:self.progressLayer]; self.progressLayer.strokeEnd = 0.0; [self addSubview:self.centerBtn];
} - (void)layoutSubviews
{
[super layoutSubviews]; [self updateLayerPath];
} #pragma mark - private - (UIButton *)centerBtn
{
if(!_centerBtn)
{
_centerBtn = [[UIButton alloc] initWithFrame:CGRectMake(, , PROGRESS_WIDTH - , PROGRESS_HEIGHT - )];
_centerBtn.center = CGPointMake(PROGRESS_WIDTH/, PROGRESS_HEIGHT/);
_centerBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
_centerBtn.layer.cornerRadius = _centerBtn.width/;
_centerBtn.backgroundColor = RGBCOLOR(, , );
_centerBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
_centerBtn.userInteractionEnabled = NO;
_centerBtn.layer.masksToBounds = YES;
}
return _centerBtn;
} - (void)updateLayerPath
{
if (_progressViewStyle == CCProgressViewStyleCircle) {
self.trackLayer.frame = self.bounds;
self.progressLayer.frame = self.bounds; CGFloat radius = CGRectGetWidth(self.frame) > CGRectGetHeight(self.frame) ?
(CGRectGetHeight(self.frame) - _lineWidth) / 2.0 : (CGRectGetWidth(self.frame) - _lineWidth) / 2.0;
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:self.progressLayer.position radius:radius startAngle:_startAngle endAngle:_clockwise ? _startAngle + * M_PI : _startAngle - * M_PI clockwise:_clockwise];
self.trackLayer.path = bezierPath.CGPath;
self.progressLayer.path = bezierPath.CGPath;
} else {
self.trackLayer.frame = CGRectMake(, (CGRectGetHeight(self.frame) - _lineWidth) / 2.0, CGRectGetWidth(self.frame), _lineWidth);
self.progressLayer.frame = self.trackLayer.frame; UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(, self.progressLayer.position.y)];
[bezierPath addLineToPoint:CGPointMake(CGRectGetWidth(self.frame), self.progressLayer.position.y)];
self.trackLayer.path = bezierPath.CGPath;
self.progressLayer.path = bezierPath.CGPath;
}
} #pragma mark - setter
- (void)setTrackTintColor:(UIColor *)trackTintColor
{
_trackTintColor = trackTintColor;
self.trackLayer.strokeColor = trackTintColor.CGColor;
} - (void)setProgressTintColor:(UIColor *)progressTintColor
{
_progressTintColor = progressTintColor;
self.progressLayer.strokeColor = progressTintColor.CGColor;
} - (void)setProgressFullTintColor:(UIColor *)progressFullTintColor
{
_progressFullTintColor = progressFullTintColor;
if (self.progressLayer.strokeEnd >= 1.0) {
self.progressLayer.strokeEnd = 1.0;
self.progressLayer.strokeColor = _progressFullTintColor.CGColor;
}
} - (void)setLineWidth:(CGFloat)lineWidth
{
_lineWidth = lineWidth;
// self.trackLayer.lineWidth = lineWidth;
self.progressLayer.lineWidth = lineWidth;
if (_progressViewStyle != CCProgressViewStyleCircle) {
[self updateLayerPath];
}
} - (void)setTrackerWidth:(CGFloat)trackerWidth {
_trackerWidth = trackerWidth;
self.trackLayer.lineWidth = _trackerWidth;
if (_progressViewStyle != CCProgressViewStyleCircle) {
[self updateLayerPath];
}
} #pragma mark - setter (CCProgressViewStyleCircle)
- (void)setFillColor:(UIColor *)fillColor
{
_fillColor = fillColor;
self.progressLayer.fillColor = fillColor.CGColor;
} - (void)setClockwise:(BOOL)clockwise
{
_clockwise = clockwise;
[self updateLayerPath];
} - (void)setStartAngle:(CGFloat)startAngle
{
_startAngle = startAngle;
[self updateLayerPath];
} - (void)setProgress:(CGFloat)progress
{
[self setProgress:progress animated:NO];
} - (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
if (animated) { // 这里的动画可以直接使用CABasicAnimation
POPBasicAnimation *basicAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
if (basicAnim) {
basicAnim.duration = kAnimTimeInterval;
basicAnim.toValue = @(progress);
} else {
basicAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
basicAnim.fromValue = @(self.progressLayer.strokeEnd);
basicAnim.toValue = @(progress);
basicAnim.duration = * kAnimTimeInterval;
basicAnim.removedOnCompletion = YES;
}
@weakify(self);
basicAnim.completionBlock = ^(POPAnimation *anim, BOOL finished) {
@strongify(self);
POPPropertyAnimation *basicAnim = (POPPropertyAnimation *)anim;
self.progressLayer.strokeEnd = [basicAnim.toValue doubleValue];
if (self.progressLayer.strokeEnd >= 1.0 && _progressFullTintColor) {
self.progressLayer.strokeEnd = 1.0;
self.progressLayer.strokeColor = _progressFullTintColor.CGColor;
}
};
[self.progressLayer pop_addAnimation:basicAnim forKey:kPOPShapeLayerStrokeEnd];
} else {
self.progressLayer.strokeEnd = progress;
if (self.progressLayer.strokeEnd >= 1.0 && _progressFullTintColor) {
self.progressLayer.strokeEnd = 1.0;
self.progressLayer.strokeColor = _progressFullTintColor.CGColor;
}
}
} @end

IOS 圆形进度条的更多相关文章

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

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

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

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

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

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

  4. 移动端纯CSS3制作圆形进度条所遇到的问题

    近日在开发的页面中,需要制作一个动态的圆形进度条,首先想到的是利用两个矩形,宽等于直径的一半,高等于直径,两个矩形利用浮动贴在一起,设置overflow:hidden属性,作为盒子,内部有一个与其宽高 ...

  5. android 自定义控件——(四)圆形进度条

    ----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...

  6. WPF 实现圆形进度条

    项目中用到圆形进度条,首先就想到使用 ProgressBar 扩展一个,在园子里找到迷途的小榔头给出的思路和部分代码,自己加以实现. 进度小于60显示红色,大于60则显示绿色.效果如下: 基本思路: ...

  7. html5 svg 圆形进度条

    html5 svg 圆形进度条 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  8. canvas圆形进度条

    通过定义一个canvas标签, new方法传进ID值,和旋转角度值,即可生成圆形进度条 <!DOCTYPE html> <html lang="en"> & ...

  9. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

随机推荐

  1. C#实现无物理边距 可打印区域的绘图\打印 z

    经常在开发实际的应用程序中,需要用到图形绘制和打印程序.如何实现完整的精确打印和绘图是需要注意许多细节地方的.最近在遇到打印问题的时候,仔细研究一阵,总结这篇博文,写得有点杂乱,看文要还请费点神. 基 ...

  2. 两个UITableView的级联菜单

    效果 源码 https://github.com/YouXianMing/Animations 说明 1. TwoLevelLinkageView封装了两个tableView,左边tableView中 ...

  3. .Net转Java.02.数据类型

    .NET中常见的数据类型分类分别是值类型和引用类型 值类型包括(基元类型.struct.枚举) 引用类型包括(类.类.数组.接口.指针) Java分为,基本类型和类   C#   Java   值类型 ...

  4. EasyMock 简单使用

    参考案例:(本位使用markdown编写)https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/https://www.yii ...

  5. Webhook 实践 —— 自动部署

    https://segmentfault.com/a/1190000007892407 安装nodejs 安装nodejs建议直接下载二进制包,把官网上的64位二进制版本下载地址复制下来,执行 wge ...

  6. java包名命名规范[【转】

    indi : 个体项目,指个人发起,但非自己独自完成的项目,可公开或私有项目,copyright主要属于发起者. 包名为“indi.发起者名.项目名.模块名.……”. pers : 个人项目,指个人发 ...

  7. 单片机成长之路(avr基础篇)- 003 AVR单片机的BOOT区

    BOOT区的由来基于一个简单的道理,即单片机的程序是保存在FLASH中的,要运行程序就必须不停的访问FLASH存储器.对于一般的FLASH存储器,数据的写入需要一定的时间来完成,在数据写入完成之前,存 ...

  8. invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    Mac系统升级git会找不到并且报错:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) ...

  9. Could not resolve all dependencies for configuration ':classpath'

    我这里是copy过来的项目包名没有修改,导致依赖找不到

  10. 【原创 Hadoop&Spark 动手实践 13】Spark综合案例:简易电影推荐系统

    [原创 Hadoop&Spark 动手实践 13]Spark综合案例:简易电影推荐系统