IOS贝塞尔曲线圆形进度条和加载动画
2023-09-14 08:57:59 时间
做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加。其中主要用到贝塞尔曲线。UIBezierPath是对CGContextRef的进一步封装,不多说直接上代码:
#import UIKit/UIKit.h @interface CircleLoader : UIView //进度颜色 @property(nonatomic, retain) UIColor* progressTintColor ;调用:
_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 () @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 - Quartz 2D 贝塞尔曲线 1、贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线。一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支点,线段像可伸缩的皮筋,我们在绘图工具上看到的钢笔工具就是来做这种矢量曲线的。
p "author-img" src="https://ucc.alicdn.com/avatar/img_06f157c6c7fcd827543d1098fa1982ae.jpg?x-oss-process=image/resize,h_150,m_lfit" />
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.
相关文章
- iOS小技能:链式编程在iOS开发中的应用
- iOS动画小知识:定点缩放弹窗(利用锚点anchorPoint进行实现)包含完整demo
- iOS小技能:iOS14相册权限适配 (Limited Photo Library Access)「建议收藏」
- iOS 获取本地图片尺寸_ios图片处理软件
- iOS Facebook和Google登录
- iOS呼吸动画
- iOS App上架App Store详细教程编辑切换为居中添加图片注释,不超过 140 字(可选)注意:下载AU后需安装java8运行
- HBuilder X 连接苹果手机(IOS)详细教程。Windows: 连接iOS手机调试项目
- IOS屏幕旋转详解手机开发
- iOS TableViewCell 加载时的动画详解手机开发
- ios常用动画详解手机开发
- ios基础:iOS改变状态栏字体颜色详解手机开发
- . ios谁是智能手机三巨头?(安卓.linux)
- 操作系统开启iOS时代:Linux操作系统的新篇章(ios是linux)
- iOS与Linux:新老技术的融合(ios与linux的关系)
- Linux与iOS:越来越亲密的合作伙伴(linux与ios)
- 微软更新iOS版OneDrive解决文件搜索故障和扫描创建PDF文件的问题等
- 苹果奇怪的问题+1:当iOS时区设置为黎巴嫩贝鲁特时闹钟无限崩溃
- 基于Linux环境下iOS应用开发实践(linux下开发ios)
- iOS开发与Linux的技术融合(ios开发linux)
- 使用使用Oracle iOS轻松操作(oracleios)
- iOS系统下的Oracle数据库环境(ios oracle)
- iOS 15/watchOS 8 正式版发布后,微软 Outlook 就会放弃支持 iOS 13/watchOS 6