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

首先看一下效果图:

下面贴上代码:

控制器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 进度条、加载、安装动画的简单实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • iOS动画解析之圆球加载动画XLBallLoading的实现

    前言 当网页的页面大小较大,用户加载可能需要较长的时间,在这些情况下,我们一般会用到(加载)loading动画,提示于用户页面在加载中,本文将详细给大家介绍关于iOS圆球加载动画XLBallLoading实现的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 一.显示效果 二.原理分析 1.拆解动画 从效果图来看,动画可拆解成两部分:放大动画.位移动画 放大动画 比较简单,这里主要来分析一下位移动画 (1).先去掉缩放效果: 屏蔽放大效果 (2).去掉其中的一个圆球 现

  • iOS利用CALayer实现动画加载的效果

    首先来看看效果图 实现过程如下 控制器调用就一句代码: [self showLoadingInView:self.view]; 方便控制器如此调用,就要为控制器添加一个分类 .h文件 #import <UIKit/UIKit.h> #import "GQCircleLoadView.h" @interface UIViewController (GQCircleLoad) //显示动画 - (void)showLoadingInView:(UIView*)view; //隐

  • iOS 实现简单的加载等待动画示例(思路与实现)

    先看下最后基本要实现的效果 总结一下自己的实现思路与所用到的类 1.这个肯定是要自定义的View类,起名为XDColorCircle吧,最后用的时候达到这样的效果 //创建XDColorCircle的实例化对象 XDColorCircle *circle=[[XDColorCircle alloc]initWithFrame:CGRectMake(0 ,100,self.view.frame.size.width,200)]; //添加到视图上展示 [self.view addSubview:c

  • 简单实现js进度条加载效果

    本文实例为大家分享了js进度条加载效果的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>下载进度</title> <style> /*定义父容器*/ .content{ width: 500px; height: 200px; background: pin

  • Android 自定义view实现进度条加载效果实例代码

    这个其实很简单,思路是这样的,就是拿view的宽度,除以点的点的宽度+二个点 之间的间距,就可以算出大概能画出几个点出来,然后就通过canvas画出点,再然后就是每隔多少时间把上面移动的点不断的去改变它的坐标就可以, 效果如下: 分析图: 代码如下: package com.example.dotloadview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bit

  • python实现文本进度条 程序进度条 加载进度条 单行刷新功能

    python实现文本进度条 程序进度条 加载进度条 单行刷新功能,具体内容如下所示: 利用time库来替代某个程序 的进行过程,做实例, 思路是,简单打印出来程序进度 单行刷新关键是\r, python默认是print后换行,所以加一个\r是光标回退到之前位置 import time tm=10 print('{:-^18}'.format('开始')) for i in range(tm+1): a='#'*i b='.'*(tm-i) c=(i/tm)*100 print('\r{:^3.0

  • 网页加载时页面显示进度条加载完成之后显示网页内容

    现在网上有很多网页加载进度条 ,但大多都是时间固定的. 下面的当查询大量数据时,网页加载较慢,在网页加载时,显示进度条,当网页加载完成时,进度条消失,显示网页已经加载完成的内容. 复制代码 代码如下: <html> <script language=VBScript> Dim Bar, SP Bar = 0 SP = 100 Function Window_onLoad() Bar = 95 SP = 10 End Function Function Count() if Bar

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

    首先看一下效果图: 下面贴上代码: 控制器ViewController: #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end /*** ---------------分割线--------------- ***/ #import "ViewController.h" #import "HWWaveView.h" #import "HWCircleVi

  • Android实现仿iOS菊花加载圈动画效果

    常见的实现方式 切图,做旋转动画 自定义View,绘制效果 gif图 1.切图会增加体积,但相对简单,不过在换肤的场景下,会使用不同颜色,需要准备多张图,不够灵活. 2.由于自定义的好处,不同颜色只需要提供自定义属性,换肤时切换属性设置即可,比较灵活. 3.gif图普遍比较大,而且加载gif没有原生支持,需要引入第三方库,而且消耗内存比较大,不推荐. 效果图: 完整代码 自定义属性: <?xml version="1.0" encoding="utf-8"?&

  • IOS开发中加载大量网络图片优化方法

    IOS开发中加载大量网络图片如何优化 1.概述 在IOS下通过URL读一张网络图片并不像其他编程语言那样可以直接把图片路径放到图片路径的位置就ok,而是需要我们通过一段类似流的方式去加载网络图片,接着才能把图片放入图片路径显示.比如: -(UIImage *) getImageFromURL:(NSString *)fileURL { //NSLog(@"执行图片下载函数"); UIImage * result; NSData * data = [NSData dataWithCont

  • PHP + plupload.js实现多图上传并显示进度条加删除实例代码

    PHP + plupload.js JS插件实现多图上传并显示进度条加删除实例,废话不多说,直接上代码 HTML代码: <!DOCTYPE html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no&qu

  • IOS中Weex 加载 .xcassets 中的图片资源的实例详解

    IOS中Weex 加载 .xcassets 中的图片资源的实例详解 前言: 因为 .xcassets 中的图片资源只能通过 imageNamed: 方法加载,所以需要做一些特殊处理,才能提供给 Weex 使用(PS:纯属娱乐,因为 Weex 跨平台的特性,这种针对某一端做实现的方案实用价值并不大). 方案 观察 WeexSDK 发现有 WXImgLoaderProtocol 这个协议,这个协议包含了下面的方法: - (id<WXImageOperationProtocol>)downloadI

  • IOS中UIWebView加载Loading的实现方法

    第一种方法:使用UIView and UIActivityIndicatorView 复制代码 代码如下: //创建UIWebView WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)]; [WebView setUserInteractionEnabled:NO]; [WebView setBackgroundColor:[UIColor clearColor]]; [WebView setDelega

随机推荐