iOS 进度条、加载、安装动画的简单实现

发布时间 - 2026-01-10 23:28:44    点击率:

首先看一下效果图:

下面贴上代码:

控制器ViewController:

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 
@end 
/*** ---------------分割线--------------- ***/ 
#import "ViewController.h" 
#import "HWWaveView.h" 
#import "HWCircleView.h" 
#import "HWProgressView.h" 
#import "HWInstallView.h" 
@interface ViewController () 
@property (nonatomic, strong) NSTimer *timer; 
@property (nonatomic, weak) HWWaveView *waveView; 
@property (nonatomic, weak) HWCircleView *circleView; 
@property (nonatomic, weak) HWProgressView *progressView; 
@property (nonatomic, weak) HWInstallView *installView; 
@end 
@implementation ViewController 
- (void)viewDidLoad { 
 [super viewDidLoad]; 
 //创建控件 
 [self creatControl]; 
 //添加定时器 
 [self addTimer]; 
} 
- (void)creatControl 
{ 
 //波浪 
 HWWaveView *waveView = [[HWWaveView alloc] initWithFrame:CGRectMake(30, 100, 150, 150)]; 
 [self.view addSubview:waveView]; 
 self.waveView = waveView; 
 //圆圈 
 HWCircleView *circleView = [[HWCircleView alloc] initWithFrame:CGRectMake(220, 100, 150, 150)]; 
 [self.view addSubview:circleView]; 
 self.circleView = circleView; 
 //进度条 
 HWProgressView *progressView = [[HWProgressView alloc] initWithFrame:CGRectMake(30, 365, 150, 20)]; 
 [self.view addSubview:progressView]; 
 self.progressView = progressView; 
 //加载安装效果 
 HWInstallView *installView = [[HWInstallView alloc] initWithFrame:CGRectMake(220, 300, 150, 150)]; 
 [self.view addSubview:installView]; 
 self.installView = installView; 
} 
- (void)addTimer 
{ 
 _timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; 
 [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; 
} 
- (void)timerAction 
{ 
 _waveView.progress += 0.01; 
 _circleView.progress += 0.01; 
 _progressView.progress += 0.01; 
 _installView.progress += 0.01; 
 if (_waveView.progress >= 1) { 
  [self removeTimer]; 
  NSLog(@"完成"); 
 } 
} 
- (void)removeTimer 
{ 
 [_timer invalidate]; 
 _timer = nil; 
} 
@end 
波浪HWWaveView:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
#import <UIKit/UIKit.h> 
@interface HWWaveView : UIView 
@property (nonatomic, assign) CGFloat progress; 
@end 
/*** ---------------分割线--------------- ***/ 
#import "HWWaveView.h" 
#define KHWWaveFillColor [UIColor groupTableViewBackgroundColor] //填充颜色 
#define KHWWaveTopColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1.0f] //前面波浪颜色 
#define KHWWaveBottomColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:0.4f] //后面波浪颜色 
@interface HWWaveView () 
@property (nonatomic, strong) CADisplayLink *displayLink; 
@property (nonatomic, assign) CGFloat wave_amplitude;//振幅a(y = asin(wx+φ) + k) 
@property (nonatomic, assign) CGFloat wave_cycle;//周期w 
@property (nonatomic, assign) CGFloat wave_h_distance;//两个波水平之间偏移 
@property (nonatomic, assign) CGFloat wave_v_distance;//两个波竖直之间偏移 
@property (nonatomic, assign) CGFloat wave_scale;//水波速率 
@property (nonatomic, assign) CGFloat wave_offsety;//波峰所在位置的y坐标 
@property (nonatomic, assign) CGFloat wave_move_width;//移动的距离,配合速率设置 
@property (nonatomic, assign) CGFloat wave_offsetx;//偏移 
@property (nonatomic, assign) CGFloat offsety_scale;//上升的速度 
@end 
@implementation HWWaveView 
- (instancetype)initWithFrame:(CGRect)frame 
{ 
 if (self = [super initWithFrame:frame]) { 
  self.backgroundColor = [UIColor clearColor]; 
  //初始化信息 
  [self initInfo]; 
 } 
 return self; 
} 
- (void)initInfo 
{ 
 //进度 
 _progress = 0; 
 //振幅 
 _wave_amplitude = self.frame.size.height / 25; 
 //周期 
 _wave_cycle = 22 * M_PI / (self.frame.size.width * 0.9); 
 //两个波水平之间偏移 
 _wave_h_distance = 22 * M_PI / _wave_cycle * 0.6; 
 //两个波竖直之间偏移 
 _wave_v_distance = _wave_amplitude * 0.4; 
 //移动的距离,配合速率设置 
 _wave_move_width = 0.5; 
 //水波速率 
 _wave_scale = 0.4; 
 //上升的速度 
 _offsety_scale = 0.1; 
 //波峰所在位置的y坐标,刚开始的时候_wave_offsety是最大值 
 _wave_offsety = (1 - _progress) * (self.frame.size.height + 22 * _wave_amplitude); 
 [self addDisplayLinkAction]; 
} 
- (void)addDisplayLinkAction 
{ 
 _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)]; 
 [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; 
} 
- (void)displayLinkAction 
{ 
 _wave_offsetx += _wave_move_width * _wave_scale; 
 //完成 
 if (_wave_offsety <= 0.01) [self removeDisplayLinkAction]; 
 [self setNeedsDisplay]; 
} 
- (void)removeDisplayLinkAction 
{ 
 [_displayLink invalidate]; 
 _displayLink = nil; 
} 
- (void)drawRect:(CGRect)rect 
{ 
 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect]; 
 [KHWWaveFillColor setFill]; 
 [path fill]; 
 [path addClip]; 
 //绘制两个波形图 
 [self drawWaveColor:KHWWaveTopColor offsetx:0 offsety:0]; 
 [self drawWaveColor:KHWWaveBottomColor offsetx:_wave_h_distance offsety:_wave_v_distance]; 
} 
- (void)drawWaveColor:(UIColor *)color offsetx:(CGFloat)offsetx offsety:(CGFloat)offsety 
{ 
 //波浪动画,进度的实际操作范围是,多加上两个振幅的高度,到达设置进度的位置y 
 CGFloat end_offY = (1 - _progress) * (self.frame.size.height + 22 * _wave_amplitude); 
 if (_wave_offsety != end_offY) { 
  if (end_offY < _wave_offsety) { 
   _wave_offsety = MAX(_wave_offsety -= (_wave_offsety - end_offY) * _offsety_scale, end_offY); 
  }else { 
   _wave_offsety = MIN(_wave_offsety += (end_offY - _wave_offsety) * _offsety_scale, end_offY); 
  } 
 } 
 UIBezierPath *wavePath = [UIBezierPath bezierPath]; 
 for (float next_x = 0.f; next_x <= self.frame.size.width; next_x ++) { 
  //正弦函数,绘制波形 
  CGFloat next_y = _wave_amplitude * sin(_wave_cycle * next_x + _wave_offsetx + offsetx / self.bounds.size.width * 22 * M_PI) + _wave_offsety + offsety; 
  if (next_x == 0) { 
   [wavePath moveToPoint:CGPointMake(next_x, next_y - _wave_amplitude)]; 
  }else { 
   [wavePath addLineToPoint:CGPointMake(next_x, next_y - _wave_amplitude)]; 
  } 
 } 
 [wavePath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)]; 
 [wavePath addLineToPoint:CGPointMake(0, self.bounds.size.height)]; 
 [color set]; 
 [wavePath fill]; 
} 
@end 
圆圈HWCircleView:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
#import <UIKit/UIKit.h> 
@interface HWCircleView : UIView 
@property (nonatomic, assign) CGFloat progress; 
@end 
/*** ---------------分割线--------------- ***/ 
#import "HWCircleView.h" 
#define KHWCircleLineWidth 10.0f 
#define KHWCircleFont [UIFont boldSystemFontOfSize:26.0f] 
#define KHWCircleColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1] 
@interface HWCircleView () 
@property (nonatomic, weak) UILabel *cLabel; 
@end 
@implementation HWCircleView 
- (instancetype)initWithFrame:(CGRect)frame 
{ 
 if (self = [super initWithFrame:frame]) { 
  self.backgroundColor = [UIColor clearColor]; 
  //百分比标签 
  UILabel *cLabel = [[UILabel alloc] initWithFrame:self.bounds]; 
  cLabel.font = KHWCircleFont; 
  cLabel.textColor = KHWCircleColor; 
  cLabel.textAlignment = NSTextAlignmentCenter; 
  [self addSubview:cLabel]; 
  self.cLabel = cLabel; 
 } 
 return self; 
} 
- (void)setProgress:(CGFloat)progress 
{ 
 _progress = progress; 
 _cLabel.text = [NSString stringWithFormat:@"%d%%", (int)floor(progress * 100)]; 
 [self setNeedsDisplay]; 
} 
- (void)drawRect:(CGRect)rect 
{ 
 //路径 
 UIBezierPath *path = [[UIBezierPath alloc] init]; 
 //线宽 
 path.lineWidth = KHWCircleLineWidth; 
 //颜色 
 [KHWCircleColor set]; 
 //拐角 
 path.lineCapStyle = kCGLineCapRound; 
 path.lineJoinStyle = kCGLineJoinRound; 
 //半径 
 CGFloat radius = (MIN(rect.size.width, rect.size.height) - KHWCircleLineWidth) * 0.5; 
 //画弧(参数:中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针) 
 [path addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + M_PI * 22 * _progress clockwise:YES]; 
 //连线 
 [path stroke]; 
} 
@end 
进度条HWProgressView:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
#import <UIKit/UIKit.h> 
@interface HWProgressView : UIView 
@property (nonatomic, assign) CGFloat progress; 
@end 
/*** ---------------分割线--------------- ***/ 
#import "HWProgressView.h" 
#define KProgressBorderWidth 2.0f 
#define KProgressPadding 1.0f 
#define KProgressColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1] 
@interface HWProgressView () 
@property (nonatomic, weak) UIView *tView; 
@end 
@implementation HWProgressView 
- (instancetype)initWithFrame:(CGRect)frame 
{ 
 if (self = [super initWithFrame:frame]) { 
  //边框 
  UIView *borderView = [[UIView alloc] initWithFrame:self.bounds]; 
  borderView.layer.cornerRadius = self.bounds.size.height * 0.5; 
  borderView.layer.masksToBounds = YES; 
  borderView.backgroundColor = [UIColor whiteColor]; 
  borderView.layer.borderColor = [KProgressColor CGColor]; 
  borderView.layer.borderWidth = KProgressBorderWidth; 
  [self addSubview:borderView]; 
  //进度 
  UIView *tView = [[UIView alloc] init]; 
  tView.backgroundColor = KProgressColor; 
  tView.layer.cornerRadius = (self.bounds.size.height - (KProgressBorderWidth + KProgressPadding) * 2) * 0.5; 
  tView.layer.masksToBounds = YES; 
  [self addSubview:tView]; 
  self.tView = tView; 
 } 
 return self; 
} 
- (void)setProgress:(CGFloat)progress 
{ 
 _progress = progress; 
 CGFloat margin = KProgressBorderWidth + KProgressPadding; 
 CGFloat maxWidth = self.bounds.size.width - margin * 2; 
 CGFloat heigth = self.bounds.size.height - margin * 2; 
 _tView.frame = CGRectMake(margin, margin, maxWidth * progress, heigth); 
} 
@end 
加载安装效果HWInstallView:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
#import <UIKit/UIKit.h> 
@interface HWInstallView : UIView 
@property (nonatomic, assign) CGFloat progress; 
@end 
/*** ---------------分割线--------------- ***/ 
#import "HWInstallView.h" 
#define KHWInstallViewMargin 10 
#define KHWInstallColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1] 
@implementation HWInstallView 
- (instancetype)initWithFrame:(CGRect)frame 
{ 
 if (self = [super initWithFrame:frame]) { 
  self.backgroundColor = [UIColor clearColor]; 
 } 
 return self; 
} 
- (void)setProgress:(CGFloat)progress 
{ 
 _progress = progress; 
 [self setNeedsDisplay]; 
} 
- (void)drawRect:(CGRect)rect 
{ 
 CGContextRef context = UIGraphicsGetCurrentContext(); 
 CGFloat xCenter = rect.size.width * 0.5; 
 CGFloat yCenter = rect.size.height * 0.5; 
 CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - KHWInstallViewMargin; 
 //背景遮罩 
 [KHWInstallColor set]; 
 CGFloat lineW = MAX(rect.size.width, rect.size.height) * 0.5; 
 CGContextSetLineWidth(context, lineW); 
 CGContextAddArc(context, xCenter, yCenter, radius + lineW * 0.5 + 5, 0, M_PI * 2, 1); 
 CGContextStrokePath(context); 
 //进程圆 
 CGContextSetLineWidth(context, 1); 
 CGContextMoveToPoint(context, xCenter, yCenter); 
 CGContextAddLineToPoint(context, xCenter, 0); 
 CGFloat endAngle = - M_PI * 0.5 + _progress * M_PI * 2 + 0.001; 
 CGContextAddArc(context, xCenter, yCenter, radius, - M_PI * 0.5, endAngle, 1); 
 CGContextFillPath(context); 
} 
@end 

以上所述是小编给大家介绍的iOS 进度条、加载、安装动画的简单实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# ios  # 进度条  # ios进度条加载安装  # iOS实现步骤进度条功能实例代码  # 使用axios实现上传图片进度条功能  # iOS中利用CoreAnimation实现一个时间的进度条效果  # ios开发加载webview显示进度条实例  # Android仿IOS ViewPager滑动进度条  # iOS实现带动画的环形进度条  # iOS快速实现环形渐变进度条  # iOS中使用NSProgress类来创建UI进度条的方法详解  # IOS实现简单的进度条功能  # iOS中WKWebView仿微信加载进度条  # 分割线  # 加载  # 小编  # 在此  # 给大家  # 刚开始  # 看一下  # 贴上  # 多加  # 所述  # 给我留言  # 感谢大家  # 实际操作  # 顺时针  # 疑问请  # 有任何  # assign  # CGFloat  # CODE 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251811 】 【 AI营销90571


相关推荐: 百度输入法ai组件怎么删除 百度输入法ai组件移除工具  网站制作大概多少钱一个,做一个平台网站大概多少钱?  Laravel定时任务怎么设置_Laravel Crontab调度器配置  实例解析angularjs的filter过滤器  JavaScript如何实现类型判断_typeof和instanceof有什么区别  手机网站制作平台,手机靓号代理商怎么制作属于自己的手机靓号网站?  如何在景安云服务器上绑定域名并配置虚拟主机?  Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】  iOS验证手机号的正则表达式  新三国志曹操传主线渭水交兵攻略  canvas 画布在主流浏览器中的尺寸限制详细介绍  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明  Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能  如何在七牛云存储上搭建网站并设置自定义域名?  非常酷的网站设计制作软件,酷培ai教育官方网站?  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  手机软键盘弹出时影响布局的解决方法  Laravel怎么清理缓存_Laravel optimize clear命令详解  Laravel如何优雅地处理服务层_在Laravel中使用Service层和Repository层  米侠浏览器网页背景异常怎么办 米侠显示修复  如何快速搭建高效简练网站?  LinuxCD持续部署教程_自动发布与回滚机制  javascript如何操作浏览器历史记录_怎样实现无刷新导航  Laravel如何使用Service Container和依赖注入?(代码示例)  制作企业网站建设方案,怎样建设一个公司网站?  浅谈Javascript中的Label语句  Laravel Debugbar怎么安装_Laravel调试工具栏配置指南  Swift中循环语句中的转移语句 break 和 continue  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  原生JS实现图片轮播切换效果  合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?  网站制作壁纸教程视频,电脑壁纸网站?  如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程  黑客如何利用漏洞与弱口令入侵网站服务器?  如何实现javascript表单验证_正则表达式有哪些实用技巧  html5的keygen标签为什么废弃_替代方案说明【解答】  Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理  使用豆包 AI 辅助进行简单网页 HTML 结构设计  JS弹性运动实现方法分析  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  Laravel如何与Vue.js集成_Laravel + Vue前后端分离项目搭建指南  详解MySQL数据库的安装与密码配置  js实现获取鼠标当前的位置  百度浏览器网页无法复制文字怎么办 百度浏览器复制修复  如何快速完成中国万网建站详细流程?  如何在建站之星绑定自定义域名?