热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:iOS进度条加载安装动画——HERO博客

篇首语:本文由编程笔记#小编为大家整理,主要介绍了iOS进度条加载安装动画——HERO博客相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了iOS 进度条加载安装动画 —— HERO博客相关的知识,希望对你有一定的参考价值。



ios 进度条、加载、安装动画简单实现。

首先看一下效果图:


下面贴上代码:

控制器ViewController:

#import
@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:

#import
@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 = 2 * M_PI / (self.frame.size.width * 0.9);
//两个波水平之间偏移
_wave_h_distance = 2 * 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 + 2 * _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 <&#61; 0.01) [self removeDisplayLinkAction];

[self setNeedsDisplay];
}
- (void)removeDisplayLinkAction
{
[_displayLink invalidate];
_displayLink &#61; nil;
}
- (void)drawRect:(CGRect)rect
{
UIBezierPath *path &#61; [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
{
//波浪动画&#xff0c;进度的实际操作范围是&#xff0c;多加上两个振幅的高度&#xff0c;到达设置进度的位置y
CGFloat end_offY &#61; (1 - _progress) * (self.frame.size.height &#43; 2 * _wave_amplitude);
if (_wave_offsety !&#61; end_offY) {
if (end_offY <_wave_offsety) {
_wave_offsety &#61; MAX(_wave_offsety -&#61; (_wave_offsety - end_offY) * _offsety_scale, end_offY);
}else {
_wave_offsety &#61; MIN(_wave_offsety &#43;&#61; (end_offY - _wave_offsety) * _offsety_scale, end_offY);
}
}

UIBezierPath *wavePath &#61; [UIBezierPath bezierPath];
for (float next_x &#61; 0.f; next_x <&#61; self.frame.size.width; next_x &#43;&#43;) {
//正弦函数&#xff0c;绘制波形
CGFloat next_y &#61; _wave_amplitude * sin(_wave_cycle * next_x &#43; _wave_offsetx &#43; offsetx / self.bounds.size.width * 2 * M_PI) &#43; _wave_offsety &#43; offsety;
if (next_x &#61;&#61; 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];
}
&#64;end

圆圈HWCircleView&#xff1a;

#import
&#64;interface HWCircleView : UIView
&#64;property (nonatomic, assign) CGFloat progress;
&#64;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]
&#64;interface HWCircleView ()
&#64;property (nonatomic, weak) UILabel *cLabel;
&#64;end
&#64;implementation HWCircleView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self &#61; [super initWithFrame:frame]) {
self.backgroundColor &#61; [UIColor clearColor];

//百分比标签
UILabel *cLabel &#61; [[UILabel alloc] initWithFrame:self.bounds];
cLabel.font &#61; KHWCircleFont;
cLabel.textColor &#61; KHWCircleColor;
cLabel.textAlignment &#61; NSTextAlignmentCenter;
[self addSubview:cLabel];
self.cLabel &#61; cLabel;
}
return self;
}
- (void)setProgress:(CGFloat)progress
{
_progress &#61; progress;
_cLabel.text &#61; [NSString stringWithFormat:&#64;"%d%%", (int)floor(progress * 100)];

[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
//路径
UIBezierPath *path &#61; [[UIBezierPath alloc] init];
//线宽
path.lineWidth &#61; KHWCircleLineWidth;
//颜色
[KHWCircleColor set];
//拐角
path.lineCapStyle &#61; kCGLineCapRound;
path.lineJoinStyle &#61; kCGLineJoinRound;
//半径
CGFloat radius &#61; (MIN(rect.size.width, rect.size.height) - KHWCircleLineWidth) * 0.5;
//画弧&#xff08;参数&#xff1a;中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针&#xff09;
[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 &#43; M_PI * 2 * _progress clockwise:YES];
//连线
[path stroke];
}
&#64;end

进度条HWProgressView&#xff1a;

#import
&#64;interface HWProgressView : UIView
&#64;property (nonatomic, assign) CGFloat progress;
&#64;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]
&#64;interface HWProgressView ()
&#64;property (nonatomic, weak) UIView *tView;
&#64;end
&#64;implementation HWProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self &#61; [super initWithFrame:frame]) {
//边框
UIView *borderView &#61; [[UIView alloc] initWithFrame:self.bounds];
borderView.layer.cornerRadius &#61; self.bounds.size.height * 0.5;
borderView.layer.masksToBounds &#61; YES;
borderView.backgroundColor &#61; [UIColor whiteColor];
borderView.layer.borderColor &#61; [KProgressColor CGColor];
borderView.layer.borderWidth &#61; KProgressBorderWidth;
[self addSubview:borderView];

//进度
UIView *tView &#61; [[UIView alloc] init];
tView.backgroundColor &#61; KProgressColor;
tView.layer.cornerRadius &#61; (self.bounds.size.height - (KProgressBorderWidth &#43; KProgressPadding) * 2) * 0.5;
tView.layer.masksToBounds &#61; YES;
[self addSubview:tView];
self.tView &#61; tView;
}

return self;
}
- (void)setProgress:(CGFloat)progress
{
_progress &#61; progress;

CGFloat margin &#61; KProgressBorderWidth &#43; KProgressPadding;
CGFloat maxWidth &#61; self.bounds.size.width - margin * 2;
CGFloat heigth &#61; self.bounds.size.height - margin * 2;

_tView.frame &#61; CGRectMake(margin, margin, maxWidth * progress, heigth);
}
&#64;end

加载安装效果HWInstallView&#xff1a;

#import
&#64;interface HWInstallView : UIView
&#64;property (nonatomic, assign) CGFloat progress;
&#64;end
/*** ---------------分割线--------------- ***/
#import "HWInstallView.h"
#define KHWInstallViewMargin 10
#define KHWInstallColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1]
&#64;implementation HWInstallView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self &#61; [super initWithFrame:frame]) {
self.backgroundColor &#61; [UIColor clearColor];
}

return self;
}
- (void)setProgress:(CGFloat)progress
{
_progress &#61; progress;

[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context &#61; UIGraphicsGetCurrentContext();

CGFloat xCenter &#61; rect.size.width * 0.5;
CGFloat yCenter &#61; rect.size.height * 0.5;
CGFloat radius &#61; MIN(rect.size.width, rect.size.height) * 0.5 - KHWInstallViewMargin;

//背景遮罩
[KHWInstallColor set];
CGFloat lineW &#61; MAX(rect.size.width, rect.size.height) * 0.5;
CGContextSetLineWidth(context, lineW);
CGContextAddArc(context, xCenter, yCenter, radius &#43; lineW * 0.5 &#43; 5, 0, M_PI * 2, 1);
CGContextStrokePath(context);

//进程圆
CGContextSetLineWidth(context, 1);
CGContextMoveToPoint(context, xCenter, yCenter);
CGContextAddLineToPoint(context, xCenter, 0);
CGFloat endAngle &#61; - M_PI * 0.5 &#43; _progress * M_PI * 2 &#43; 0.001;
CGContextAddArc(context, xCenter, yCenter, radius, - M_PI * 0.5, endAngle, 1);
CGContextFillPath(context);
}
&#64;end


HWProgress Demo 下载链接&#xff1a;http://code.cocoachina.com/view/134374


猜你喜欢&#xff1a;iOS 绘图机制简介&#xff0c;Quartz 2D绘图用CGContextRef绘制音频波形图





推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
小鱼014999
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有