iOS实现圆环比例图

 更新时间:2020年11月26日 10:04:12   作者:欧生1539  
这篇文章主要为大家详细介绍了iOS实现圆环比例图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了iOS实现圆环比例图的具体代码,供大家参考,具体内容如下

实现效果

实现方法

1. SSTCircleProgressView 

@interface SSTCircleProgressView : UIView 
/**
 *进度条的角的类型
 */
@property (nonatomic,copy) CAShapeLayerLineCap lineCap;
 
/**
 *进度条显示的文字
 */
@property (nonatomic,copy) NSString *progressLabelText;
 
/**
 *进度条显示的文字的颜色
 */
@property (nonatomic,copy) UIColor *progressLabelTextColor;
 
/**
 *进度条宽度
 */
@property (nonatomic,assign) CGFloat progressLineWidth;
/**
 * 背景线条宽度
 */
@property (nonatomic,assign) CGFloat backgroundLineWidth;
/**
 * 进度百分比
 */
@property (nonatomic,assign) CGFloat percentage;
/**
 * 背景填充颜色
 */
@property (nonatomic,strong) UIColor *backgroundStrokeColor;
/**
 * 进度条填充颜色
 */
@property (nonatomic,strong) UIColor *progressStrokeColor;
/**
 * 距离边框边距偏移量
 */
@property (nonatomic,assign) CGFloat offset;
 
- (void)setProgress:(CGFloat)percentage animated:(BOOL)animated;
 
@end
#import "SSTCircleProgressView.h"
 
#define kDuration 1.0
#define kDefaultLineWidth 10
 
@interface SSTCircleProgressView()
 
@property (nonatomic,strong) CAShapeLayer *backgroundLayer;
@property (nonatomic,strong) CAShapeLayer *progressLayer;
@property (nonatomic,strong) UILabel *progressLabel;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) CGFloat startAngle ; // M_PI*2
@property (nonatomic,assign) CGFloat endAngle ;
 
@end
 
@implementation SSTCircleProgressView
 
- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self setBackgroundColor:[UIColor clearColor]];
    [self createSubViews];
    //init default variable
    self.backgroundLineWidth = kDefaultLineWidth;
    self.progressLineWidth = kDefaultLineWidth;
    self.percentage = 0;
    self.offset = 0;
    self.startAngle = -M_PI_2;
    self.endAngle = 0;
  }
  return self;
}
 
- (void)createSubViews
{
  //self.progressLabel.text = @"0%";
  self.progressLabel.textAlignment = NSTextAlignmentCenter;
  self.progressLabel.font = FONTBOLD(12);
  [self addSubview:self.progressLabel];
  
  _backgroundLayer = [CAShapeLayer layer];
  _backgroundLayer.frame = self.bounds;
  _backgroundLayer.fillColor = nil;
  _backgroundLayer.strokeColor = [UIColor lightGrayColor].CGColor;
  
  _progressLayer = [CAShapeLayer layer];
  _progressLayer.frame = self.bounds;
  _progressLayer.fillColor = nil;
  _progressLayer.strokeColor = [UIColor redColor].CGColor;
  
  [self.layer addSublayer:_backgroundLayer];
  [self.layer addSublayer:_progressLayer];
}
 
-(void)setProgressLabelText:(NSString *)progressLabelText{
  _progressLabelText = progressLabelText;
  self.progressLabel.text = progressLabelText;
}
 
-(void)setProgressLabelTextColor:(UIColor *)progressLabelTextColor{
  _progressLabelTextColor = progressLabelTextColor;
  self.progressLabel.textColor = progressLabelTextColor;
}
 
 
#pragma mark - Draw CircleLine
- (void)setBackgroundCircleLine
{
  UIBezierPath *path = [UIBezierPath bezierPath];
  path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x - self.frame.origin.x, self.center.y - self.frame.origin.y)
                     radius:(self.frame.size.width - _backgroundLineWidth)/2 - _offset
                   startAngle:self.startAngle
                    endAngle:self.endAngle
                    clockwise:NO];
  _backgroundLayer.path = path.CGPath;
}
 
- (void)setProgressCircleLine
{
  UIBezierPath *path = [UIBezierPath bezierPath];
  path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x - self.frame.origin.x, self.center.y - self.frame.origin.y)
                     radius:(self.frame.size.width - _progressLineWidth)/2 - _offset
                   startAngle:self.startAngle
                    endAngle:self.endAngle
                    clockwise:NO];
  _progressLayer.path = path.CGPath;
}
 
#pragma mark - Lazy Load
- (UILabel *)progressLabel
{
  if (!_progressLabel) {
    _progressLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.bounds.size.width -100)/2, (self.bounds.size.height - 100)/2, 100, 100)];
  }
  return _progressLabel;
}
 
- (void)setBackgroundLineWidth:(CGFloat)backgroundLineWidth
{
  _backgroundLineWidth = backgroundLineWidth;
  _backgroundLayer.lineWidth = _backgroundLineWidth;
  [self setBackgroundCircleLine];
}
 
-(void)setLineCap:(CAShapeLayerLineCap)lineCap{
  _progressLayer.lineCap = lineCap;
  [self setProgressCircleLine];
}
 
- (void)setProgressLineWidth:(CGFloat)progressLineWidth
{
  _progressLineWidth = progressLineWidth;
  _progressLayer.lineWidth = _progressLineWidth;
  [self setProgressCircleLine];
}
 
- (void)setPercentage:(CGFloat)percentage {
  _percentage = percentage;
}
 
- (void)setBackgroundStrokeColor:(UIColor *)backgroundStrokeColor {
  _backgroundStrokeColor = backgroundStrokeColor;
  _backgroundLayer.strokeColor = _backgroundStrokeColor.CGColor;
}
 
- (void)setProgressStrokeColor:(UIColor *)progressStrokeColor
{
  _progressStrokeColor = progressStrokeColor;
  _progressLayer.strokeColor = _progressStrokeColor.CGColor;
}
 
#pragma mark - progress animated YES or NO
- (void)setProgress:(CGFloat)percentage animated:(BOOL)animated
{
  self.percentage = percentage;
  _progressLayer.strokeEnd = _percentage;
  if (animated) {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:_percentage];
    animation.duration = kDuration;
    [_progressLayer addAnimation:animation forKey:@"strokeEndAnimation"];
  }else{
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    _progressLabel.text = [NSString stringWithFormat:@"%.0f%%",_percentage*100];
    [CATransaction commit];
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • IOS开发中异步网络请求上实现同步逻辑

    IOS开发中异步网络请求上实现同步逻辑

    这篇文章主要介绍了IOS开发中异步网络请求上实现同步逻辑的相关资料,需要的朋友可以参考下
    2017-03-03
  • 举例讲解iOS应用开发中hitTest触摸事件的编写方法

    举例讲解iOS应用开发中hitTest触摸事件的编写方法

    这篇文章主要介绍了举例讲解iOS应用开发中hitTest触摸事件的编写方法,重点讲解了两个view之间的事件传递,需要的朋友可以参考下
    2016-04-04
  • iOS指纹验证TouchID应用学习教程2

    iOS指纹验证TouchID应用学习教程2

    这篇文章主要为大家详细iOS指纹验证TouchID应用学习教程的第一篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • iOS Swift逻辑运算符示例总结

    iOS Swift逻辑运算符示例总结

    运算符是一个符号,用于告诉编译器执行一个数学或逻辑运算,下面这篇文章主要给大家介绍了关于iOS Swift逻辑运算符的相关资料,需要的朋友可以参考下
    2021-10-10
  • 轻松理解iOS 11中webview的视口

    轻松理解iOS 11中webview的视口

    这篇文章主要介绍了iOS 11中webview的视口知识,需要的朋友可以参考下
    2017-09-09
  • iOS键盘弹出遮挡输入框的解决方法

    iOS键盘弹出遮挡输入框的解决方法

    这篇文章主要为大家详细介绍了iOS键盘弹出遮挡输入框的解决方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • ios中getTime()的兼容性实例代码

    ios中getTime()的兼容性实例代码

    在本篇文章里小编给大家整理的是关于ios中getTime()的兼容性实例代码,需要的朋友们可以学习下。
    2020-03-03
  • iOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自己对未知的好奇心,还经常能发现一些意外的惊喜。这篇文章主要介绍了iOS通过逆向如何深入理解Block内存模型的相关资料。
    2017-01-01
  • iOS逆向教程之动态调试详解

    iOS逆向教程之动态调试详解

    这篇文章主要给大家介绍了关于iOS逆向教程之动态调试的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • iphone的safari浏览器中实现全屏浏览的方法

    iphone的safari浏览器中实现全屏浏览的方法

    这篇文章主要介绍了iphone的safari浏览器中实现全屏浏览的方法,同时介绍了Add to Home Screen功能的实现方法,需要的朋友可以参考下
    2014-06-06

最新评论