首先看一下效果图:
下面贴上代码:
控制器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
相关文章:
公众号网站制作网页,微信公众号怎么制作?
如何通过虚拟主机空间快速建站?
南京网站制作费用,南京远驱官方网站?
香港服务器如何优化才能显著提升网站加载速度?
常州自助建站工具推荐:低成本搭建与模板选择技巧
如何快速搭建高效香港服务器网站?
制作销售网站教学视频,销售网站有哪些?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
建站之星安装后界面空白如何解决?
微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?
代购小票制作网站有哪些,购物小票的简要说明?
如何快速生成专业多端适配建站电话?
建站主机选购指南:核心配置优化与品牌推荐方案
网站制作多少钱一个,建一个论坛网站大约需要多少钱?
如何在万网主机上快速搭建网站?
如何在Windows 2008云服务器安全搭建网站?
C++中引用和指针有什么区别?(代码说明)
国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?
潮流网站制作头像软件下载,适合母子的网名有哪些?
如何在橙子建站上传落地页?操作指南详解
广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?
建站之星如何实现PC+手机+微信网站五合一建站?
制作证书网站有哪些,全国城建培训中心证书查询官网?
如何用美橙互联一键搭建多站合一网站?
如何正确下载安装西数主机建站助手?
详解jQuery中基本的动画方法
如何通过万网虚拟主机快速搭建网站?
网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?
营销式网站制作方案,销售哪个网站招聘效果最好?
盐城做公司网站,江苏电子版退休证办理流程?
岳西云建站教程与模板下载_一站式快速建站系统操作指南
寿县云建站:智能SEO优化与多行业模板快速上线指南
建站之星如何保障用户数据免受黑客入侵?
C#如何在一个XML文件中查找并替换文本内容
建站之星伪静态规则如何设置?
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
用v-html解决Vue.js渲染中html标签不被解析的问题
图册素材网站设计制作软件,图册的导出方式有几种?
建站之星免费模板:自助建站系统与智能响应式一键生成
商务网站制作工程师,从哪几个方面把握电子商务网站主页和页面的特色设计?
网站制作公司,橙子建站是合法的吗?
建站之星展会模板:智能建站与自助搭建高效解决方案
建站之星安装需要哪些步骤及注意事项?
如何在万网自助建站中设置域名及备案?
北京网站制作网页,网站升级改版需要多久?
建站之星如何快速生成多端适配网站?
建站主机选哪家性价比最高?
零服务器AI建站解决方案:快速部署与云端平台低成本实践
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
,想在网上投简历,哪几个网站比较好?
*请认真填写需求信息,我们会在24小时内与您取得联系。