带动画渐进效果与颜色渐变的圆弧进度控件设计(二)

简介: 带动画渐进效果与颜色渐变的圆弧进度控件设计

控件接口的设计:

import <UIKit/UIKit.h>

@interface YHBaseCircleView : UIView
//==============下面三个渐变色必须全部设置 否则效果可能与预期不同================//
/**
*设置圆弧渐变色的起始色
*/
@property(nonatomic,strong)UIColor * minLineColor;
/**
*设置圆弧渐变色的中间色
*/
@property(nonatomic,strong)UIColor * midLineColor;
/**
*设置圆弧渐变色的终止色
*/
@property(nonatomic,strong)UIColor * maxLineColor;
/**
*设置圆弧背景色
*/
@property(nonatomic,strong)UIColor * lineTintColor;
/**
*设置进度
*/
@property(nonatomic,assign)CGFloat progress;
/**
*设置线的宽度 max = 20 min = 0.5
*/
@property(nonatomic,assign)CGFloat lineWidth;
/**
*设置是否显示百分比标签
*/
@property(nonatomic,assign)BOOL showTipLabel;
/**
*设置百分比标签进度颜色
*/
@property(nonatomic,strong)UIColor * textColor;
/**

  • @brief 设置进度

*

  • @param progress 进度 取值0-1

*

  • @param animated 是否显示动画

*
*/
-(void)setProgress:(CGFloat)progress animated:(BOOL)animated;
@end
实现方法如下:

import "YHBaseCircleView.h"

@implementation YHBaseCircleView
{

//进度控件内容尺寸
float _contentWidth;
float _contentHeight;
//形状layer
CAShapeLayer * _shapeLayer;
//颜色渐变layer
CAGradientLayer * _gradLayerR;
CAGradientLayer * _gradLayerL;
CALayer * _gradLayer;
//内容layer
CAShapeLayer * _contentLayer;
UILabel * _tipLabel;
//专门用来更新label
NSTimer * _timer;
float _oldProgress;
//进度新旧进度值
int old;
int new;

}

-(void)awakeFromNib{

[self reloadView];

}
-(instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];
if (self) {
    [self reloadView];
}
return self;

}

-(void)reloadView{

self.backgroundColor = [UIColor clearColor];
//取设置的frame的最小长或款作为内容区域
_contentWidth = _contentHeight = CGRectGetWidth(self.frame)>CGRectGetHeight(self.frame)?CGRectGetHeight(self.frame):CGRectGetWidth(self.frame);
//创建内容layer
_contentLayer = [CAShapeLayer layer];
_contentLayer.bounds = CGRectMake(0, 0, _contentWidth, _contentHeight);
_contentLayer.position = CGPointMake(_contentWidth/2, _contentHeight/2);
_contentLayer.backgroundColor = [UIColor clearColor].CGColor;
//进行边界描绘 默认线宽为4px
UIBezierPath * pathT = [UIBezierPath bezierPathWithArcCenter:_contentLayer.position radius:_contentWidth/2-2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES];
_contentLayer.path = pathT.CGPath;
//默认填充颜色为白色
_contentLayer.fillColor = [UIColor whiteColor].CGColor;
_contentLayer.lineWidth = 4;
_contentLayer.strokeColor = [UIColor grayColor].CGColor;
[self.layer addSublayer:_contentLayer];

_shapeLayer = [CAShapeLayer layer];
_shapeLayer.bounds = CGRectMake(0, 0, _contentWidth, _contentHeight);
_shapeLayer.position = CGPointMake(_contentWidth/2, _contentHeight/2);
_shapeLayer.backgroundColor = [UIColor clearColor].CGColor;

// _shapeLayer.lineCap = kCALineCapRound;

//进行边界描绘 默认线宽为4px
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:_shapeLayer.position radius:_contentWidth/2-2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES];
_shapeLayer.path = path.CGPath;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.lineWidth = 4;
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
//默认黄转橙转红的边界线 分别由两个gradLayer进行控制
_gradLayer = [CALayer layer];
_gradLayer.bounds = _contentLayer.bounds;
_gradLayer.position = _contentLayer.position;
_gradLayer.backgroundColor = [UIColor clearColor].CGColor;
_gradLayerL = [CAGradientLayer layer];
_gradLayerL.bounds = CGRectMake(0, 0, _contentWidth/2, _contentHeight);
_gradLayerL.locations = @[@0.6];
[_gradLayerL setColors:@[(id)[UIColor redColor].CGColor,(id)[UIColor orangeColor].CGColor]];
_gradLayerL.position = CGPointMake(_gradLayerL.bounds.size.width/2, _gradLayerL.bounds.size.height/2);

[_gradLayer addSublayer:_gradLayerL];

_gradLayerR = [CAGradientLayer layer];
_gradLayerR.locations = @[@0.6];
_gradLayerR.bounds = CGRectMake(_contentWidth/2, 0, _contentWidth/2, _contentHeight);
[_gradLayerR setColors:@[(id)[UIColor yellowColor].CGColor,(id)[UIColor orangeColor].CGColor]];
_gradLayerR.position = CGPointMake(_gradLayerR.bounds.size.width/2+_contentWidth/2, _gradLayerR.bounds.size.height/2);
[_gradLayer addSublayer:_gradLayerR];
[_gradLayer setMask:_shapeLayer];
[_contentLayer addSublayer:_gradLayer];


//setter方法初始化
_minLineColor = [UIColor yellowColor];
_midLineColor = [UIColor orangeColor];
_maxLineColor = [UIColor redColor];
_lineTintColor = [UIColor grayColor];
_progress = 1;
_lineWidth = 4;
_lineTintColor = [UIColor grayColor];
_textColor = [UIColor orangeColor];
_oldProgress = 1;
//创建tiplabel
[self creatTipLabel];
_timer = [NSTimer scheduledTimerWithTimeInterval:1/60.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
_timer.fireDate = [NSDate distantFuture];

}

-(void)removeFromSuperview{

_timer.fireDate = [NSDate distantFuture];
[_timer invalidate];
_timer =nil;
[super removeFromSuperview];

}
-(void)updateLabel{

if (old<new) {
    old++;
    NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%d%%",old]];
    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, attri.length-1)];
    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(attri.length-1, 1)];
    [attri addAttribute:NSForegroundColorAttributeName value:_textColor range:NSMakeRange(0, attri.length)];
    _tipLabel.attributedText = attri;
}else if (old>new){
    old--;
    NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%d%%",old]];
    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, attri.length-1)];
    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(attri.length-1, 1)];
    [attri addAttribute:NSForegroundColorAttributeName value:_textColor range:NSMakeRange(0, attri.length)];
    _tipLabel.attributedText = attri;
}else{
    _timer.fireDate = [NSDate distantFuture];
}

}
-(void)setMinLineColor:(UIColor *)minLineColor{

_minLineColor = minLineColor;
[_gradLayerR setColors:@[(id)_minLineColor.CGColor,(id)_midLineColor.CGColor]];
[_gradLayerL setColors:@[(id)_maxLineColor.CGColor,(id)_midLineColor.CGColor]];

}
-(void)setMidLineColor:(UIColor *)midLineColor{

_midLineColor = midLineColor;
[_gradLayerR setColors:@[(id)_minLineColor.CGColor,(id)_midLineColor.CGColor]];
[_gradLayerL setColors:@[(id)_maxLineColor.CGColor,(id)_midLineColor.CGColor]];

}
-(void)setMaxLineColor:(UIColor *)maxLineColor{

_maxLineColor = maxLineColor;
[_gradLayerR setColors:@[(id)_minLineColor.CGColor,(id)_midLineColor.CGColor]];
[_gradLayerL setColors:@[(id)_maxLineColor.CGColor,(id)_midLineColor.CGColor]];

}
-(void)setTintColor:(UIColor *)tintColor{

_lineTintColor = tintColor;
_contentLayer.strokeColor = tintColor.CGColor;

}
-(void)setProgress:(CGFloat)progress{

_oldProgress = _progress;
_progress=progress;
_shapeLayer.strokeStart = 0;
_shapeLayer.strokeEnd = progress>1?1:progress;
NSMutableAttributedString * attri ;
if (progress==1) {
    attri = [[NSMutableAttributedString alloc]initWithString:@"100%"];
}else{
    attri = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%2d%%",(int)(progress*100)]];
}
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, attri.length-1)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(attri.length-1, 1)];
[attri addAttribute:NSForegroundColorAttributeName value:_textColor range:NSMakeRange(0, attri.length)];
_tipLabel.attributedText = attri;

}

-(void)setProgress:(CGFloat)progress animated:(BOOL)animated{

_oldProgress = _progress;
_progress = progress;
old = (int)(_oldProgress*100);
new = (int)(_progress*100);
CABasicAnimation * ani = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
ani.toValue = progress>1?@1:@(progress);
ani.duration = 0.3;
ani.delegate=self;
ani.fillMode=kCAFillModeForwards;
ani.removedOnCompletion=NO;
[_shapeLayer addAnimation:ani forKey:nil];
_timer.fireDate = [NSDate distantPast];

}

  • (void)dealloc

{

}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{

if (flag) {
    [_shapeLayer removeAllAnimations];
    _shapeLayer.strokeEnd = _progress>1?1:_progress;
}

}
-(void)setLineWidth:(CGFloat)lineWidth{

if (lineWidth<0.5) {
    lineWidth=0.5;
}
if (lineWidth>20) {
    lineWidth = 20;
}
_lineWidth = lineWidth;
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:_shapeLayer.position radius:_contentWidth/2-lineWidth/2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES];
_shapeLayer.path = path.CGPath;
_shapeLayer.fillColor = [UIColor clearColor].CGColor;
_shapeLayer.lineWidth = lineWidth;
_shapeLayer.strokeColor = [UIColor redColor].CGColor;
[_gradLayer setMask:_shapeLayer];
UIBezierPath * pathT = [UIBezierPath bezierPathWithArcCenter:_contentLayer.position radius:_contentWidth/2-lineWidth/2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES];
_contentLayer.path = pathT.CGPath;
_contentLayer.lineWidth = lineWidth;

}
-(void)setTextColor:(UIColor *)textColor{

_textColor = textColor;
NSMutableAttributedString * attr = [[NSMutableAttributedString alloc]initWithAttributedString:_tipLabel.attributedText];
[attr addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, attr.length)];
_tipLabel.attributedText = attr;

}
-(void)creatTipLabel{

_tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, sqrt(2)/2*(_contentWidth-_lineWidth*2), sqrt(2)/2*(_contentWidth-_lineWidth*2))];
_tipLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
_tipLabel.backgroundColor = [UIColor clearColor];
_tipLabel.textAlignment = NSTextAlignmentCenter;
NSMutableAttributedString * attri = [[NSMutableAttributedString alloc]initWithString:@"100%"];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, 3)];
[attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(3, 1)];
[attri addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, 4)];
_tipLabel.attributedText = attri;
[self addSubview:_tipLabel];

}
@end

目录
相关文章
|
编解码 算法 数据可视化
数字信号处理(DSP):原理、应用与代码实现
数字信号处理(DSP):原理、应用与代码实现
|
10月前
|
数据采集 自动驾驶 机器人
【信号处理】通过 “最近邻匹配” 和 “球面线性插值(SLERP)” 两种方式将 GNSS 位姿(位置 + 四元数插值到激光雷达时间戳附Matlab代码
✅作者简介:热爱科研的Matlab仿真开发者,擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。 🍎 往期回顾关注个人主页:Matlab科研工作室 👇 关注我领取海量matlab电子书和数学建模资料 🍊个人信条:格物致知,完整Matlab代码获取及仿真咨询内容私信。 🔥 内容介绍 1. 引言:为什么需要位姿时间戳对齐? 在自动驾驶、移动测绘、机器人导航等场景中,GNSS(全球导航卫星系统)负责提供高精度位姿(位置 X/Y/Z + 姿态四元数 Qx/Qy/Qz/Qw),激光雷达负责采集环境点云数据,二者需通过时间戳对齐才能实现点云与地理坐标的精准融合 ——
338 1
|
存储 人工智能 自然语言处理
方案介绍|基于百炼生成向量数据并使用阿里云Milvus存储和检索
阿⾥云Milvus是⼀款云上全托管服务,提供⼤规模向量数据的相似性检索服务。100%兼容开源Milvus,在开源版本的基础上增强了可扩展性,具备易⽤性、可⽤性、安全性、低成本与⽣态优势。阿⾥云Milvus可以⽀持⼏乎所有涉及到向量搜索的场景。例如检索增强⽣成RAG,以及经典的搜索推荐、多模态检索等。阿里云Milvus可存储百炼产生的向量数据,并进行大规模向量数据的检索。本文将重点介绍这一过程的方案。
1787 2
|
存储 SQL 前端开发
跟老卫学HarmonyOS开发:ArkTS关系型数据库开发
本节以“账本”为例,使用关系型数据库接口实现账单的增、删、改、查操作。通过创建ArkTSRdb应用,演示如何操作RdbStore进行数据管理,并结合界面按钮实现交互功能。
779 0
跟老卫学HarmonyOS开发:ArkTS关系型数据库开发
|
12月前
|
人工智能 C++
AnythingLLM vs Cherry Studio vs Chatbox:三大AI工具深度横评
本文深入对比三大AI工具:AnythingLLM、Cherry Studio与Chatbox。从易用性、功能丰富度、RAG能力、多模态支持到知识库管理,全面分析各工具优劣,并结合企业知识管理、个人创作学习等场景提供选型指南。最终指出:选择不在于“最好”,而在于“最合适”——AnythingLLM适合企业级知识体系构建,Cherry Studio助力多模态内容创作,Chatbox则以极简体验满足日常对话需求。
|
JSON JavaScript 前端开发
什么是HTTP POST请求?初学者指南与示范
HTTP POST请求是一种常用的HTTP方法,主要用于向服务器发送数据。通过合理设置请求头和请求主体,可以实现数据的可靠传输。无论是在客户端使用JavaScript,还是在服务器端使用Node.js,理解和掌握POST请求的工作原理和应用场景,对于Web开发至关重要。
3308 18
|
人工智能 自然语言处理 PyTorch
InspireMusic:阿里通义实验室开源的音乐生成模型,支持文本或音频生成多种风格的音乐
阿里通义实验室开源的音乐生成技术,支持通过简单描述快速生成多种风格的高质量音乐作品。
3305 4