ios弧形进度条_IOS贝塞尔曲线圆形进度条和加载动画-阿里云开发者社区
做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加。其中主要用到贝塞尔曲线。UIBezierPath是对CGContextRef的进一步封装,不多说直接上代码:
#import
@interface CircleLoader : UIView
//进度颜色
@property(nonatomic, retain) UIColor* progressTintColor ;
//轨道颜色
@property(nonatomic, retain) UIColor* trackTintColor ;
//轨道宽度
@property (nonatomic,assign) float lineWidth;
//中间图片
@property (nonatomic,strong) UIImage *centerImage;
//进度
@property (nonatomic,assign) float progressValue;
//提示标题
@property (nonatomic,strong) NSString *promptTitle;
//开启动画
@property (nonatomic,assign) BOOL animationing;
//隐藏消失
- (void)hide;
@end
#import "CircleLoader.h"
@interface CircleLoader ()
@property (nonatomic,strong) CAShapeLayer *trackLayer;
@property (nonatomic,strong) CAShapeLayer *progressLayer;
@end
@implementation CircleLoader
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor=[UIColor clearColor];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
_trackLayer=[CAShapeLayer layer];
_trackLayer.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_trackLayer.lineWidth=_lineWidth;
_trackLayer.strokeColor=_trackTintColor.CGColor;
_trackLayer.fillColor = self.backgroundColor.CGColor;
_trackLayer.lineCap = kCALineCapRound;
[self.layer addSublayer:_trackLayer];
_progressLayer=[CAShapeLayer layer];
_progressLayer.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_progressLayer.lineWidth=_lineWidth;
_progressLayer.strokeColor=_progressTintColor.CGColor;
_progressLayer.fillColor = self.backgroundColor.CGColor;
_progressLayer.lineCap = kCALineCapRound;
[self.layer addSublayer:_progressLayer];
if (_centerImage!=nil) {
UIImageView *centerImgView=[[UIImageView alloc]initWithImage:_centerImage];
centerImgView.frame=CGRectMake(_lineWidth, _lineWidth, self.frame.size.width-2*_lineWidth, self.frame.size.height-_lineWidth*2);
// centerImgView.center=self.center;
centerImgView.layer.cornerRadius=(self.frame.size.width+_lineWidth)/2;
centerImgView.clipsToBounds=YES;
[self.layer addSublayer:centerImgView.layer];
}
[self start];
}
- (void)drawBackgroundCircle:(BOOL) animationing {
//贝塞尔曲线 0度是在十字右边方向 -M_PI/2相当于在十字上边方向
CGFloat startAngle = - ((float)M_PI / 2); // 90 Degrees
//
CGFloat endAngle = (2 * (float)M_PI) + - ((float)M_PI / 8);;
CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
CGFloat radius = (self.bounds.size.width - _lineWidth)/2;
UIBezierPath *processPath = [UIBezierPath bezierPath];
// processPath.lineWidth=_lineWidth;
UIBezierPath *trackPath = [UIBezierPath bezierPath];
// trackPath.lineWidth=_lineWidth;
//---------------------------------------
// Make end angle to 90% of the progress
//---------------------------------------
if (!animationing) {
endAngle = (_progressValue * 2*(float)M_PI) + startAngle;
}
else
{
endAngle = (0.1 * 2*(float)M_PI) + startAngle;
}
[processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[trackPath addArcWithCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:YES];
_progressLayer.path = processPath.CGPath;
_trackLayer.path=trackPath.CGPath;
}
- (void)start
{
[self drawBackgroundCircle:_animationing];
if (_animationing) {
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VALF;
[_progressLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
}
- (void)hide
{
[_progressLayer removeAllAnimations];
[self removeFromSuperview];
}
@end
调用:
#import "ViewController.h"
#import "CircleLoader.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置视图大小
CircleLoader *view=[[CircleLoader alloc]initWithFrame:CGRectMake(100, 100, 70, 70)];
//设置轨道颜色
view.trackTintColor=[UIColor redColor];
//设置进度条颜色
view.progressTintColor=[UIColor greenColor];
//设置轨道宽度
view.lineWidth=5.0;
//设置进度
view.progressValue=0.7;
//设置是否转到 YES进度不用设置
view.animationing=YES;
//添加中间图片 不设置则不显示
view.centerImage=[UIImage imageNamed:@"yzp_loading"];
//添加视图
[self.view addSubview:view];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//视图隐藏
// [view hide];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果:
ios弧形进度条_IOS贝塞尔曲线圆形进度条和加载动画-阿里云开发者社区相关推荐
- python路径分隔符_Python:当读取一个没有默认分隔符的文件(包含数百万条记录)并将其放入dataframe (pa-问答-阿里云开发者社区-阿里云...
Python:在没有默认分隔符(包含数百万条记录)的情况下读取文件并将其放入"数据框架(panda)"中,最有效的方法是什么? 文件是:"file_sd.txt" ...
- IOS贝塞尔曲线圆形进度条和加载动画
做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加.其中主要用到贝塞尔曲线.UIBezierPath是对CGContextRef的进一步 ...
- oc调用python_引用ios-和引用ios相关的内容-阿里云开发者社区
iOS内存管理机制解析之MRC手动引用计数机制 前言: iOS的内存管理机制ARC和MRC是程序员参加面试基本必问的问题,也是考察一个iOS基本功是 否扎实的关键,这样深入理解内存管理机制的重要性就不 ...
- ios调用python_引用ios-和引用ios相关的内容-阿里云开发者社区
iOS内存管理机制解析之MRC手动引用计数机制 前言: iOS的内存管理机制ARC和MRC是程序员参加面试基本必问的问题,也是考察一个iOS基本功是 否扎实的关键,这样深入理解内存管理机制的重要性就不 ...
- mysql 数据库军规_MySQL 数据库开发的33 条军规-阿里云开发者社区
写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过. (一)核心军规 (1)不在数据库做运算:cpu计算务必移至业务层 (2)控制单表数据量:单表记录控制在1 ...
- opencv拖动进度条_OpenCV GUI基本操作,回调函数,进度条,裁剪图像等-阿里云开发者社区...
代码为转载,出处找不到了,不贴了 工具条进度条: // ConvertColor.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #incl ...
- ios 仿电脑qq登录界面_iOS开发UI篇—模仿ipad版QQ空间登录界面-阿里云开发者社区...
一.实现和步骤 1.一般ipad项目在命名的时候可以加一个HD,标明为高清版 2.设置项目的文件结构,分为home和login两个部分 3.登陆界面的设置 (1)设置第一个控制器和自定义的控制器类(登 ...
- flutter 调用原生安卓插件_Flutter 如何调用Android和iOS原生代码-阿里云开发者社区...
分3个大步骤: 1.在flutter中调用原生方法 2.在Android中实现被调用的方法 3.在iOS中实现被调用的方法 在flutter中调用原生方法 场景,这里你希望调用原生方法告诉你一个boo ...
- mysql数据一条复制_Mysql 复制一条数据-阿里云开发者社区
从不同的表复制 insert into 表1 select * from 表2 where id =** ; 同一张表中复制(无主键) insert into 表1 select * from 表2 ...
最新文章
- python画图三维-对python mayavi三维绘图的实现详解
- 语言检测工具-langid
- Mockito的使用(一)——@InjectMocks、@Spy、@Mock
- 从头开始搭建爬虫环境
- iPad2泄密 责任只在“内鬼”吗?
- JavaScript怎么上传图片
- Linux笔记-iptables模拟公司环境配置
- 如何把本地yum源给其他机器使用_配置本地yum源以及第3方软件仓库的搭建
- iOS-仿支付宝刮刮乐效果
- datatable某一行第N列为空的时候删除某一行
- 中国胎儿(分娩和分娩)和新生儿护理设备行业市场供需与战略研究报告
- 区块链 以太坊 每个区块可以包含多少个交易
- html5游戏开发教程实战:五子棋、四子棋、围棋、翻转棋四种对弈游戏,仅仅100行代码
- SDOI2015 星际战争
- TLC5615模数转换锯齿波仿真实验(Arduino)
- 隐函数存在定理隐函数的高阶导数
- 如何在微信小程序里面退出小程序
- 人机版五子棋两种算法概述
- 机器学习之逻辑回归(对数几率回归)
- 笔记本玩游戏画面间歇卡顿的终极解决方案 ThrottleStop使用教程