做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加。其中主要用到贝塞尔曲线。UIBezierPath是对CGContextRef的进一步封装,不多说直接上代码:

#import <UIKit/UIKit.h>

@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(, , 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(, , 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-*_lineWidth, self.frame.size.height-_lineWidth*);
//        centerImgView.center=self.center;
        centerImgView.layer.cornerRadius=(self.frame.size.width+_lineWidth)/;
        centerImgView.clipsToBounds=YES;
        [self.layer addSublayer:centerImgView.layer];
    }

    [self start];
}

- (void)drawBackgroundCircle:(BOOL) animationing {

    //贝塞尔曲线 0度是在十字右边方向   -M_PI/2相当于在十字上边方向
    CGFloat startAngle = - ((); // 90 Degrees

    //
    CGFloat endAngle = ( * ();;
    CGPoint center = CGPointMake(self.bounds.size.width/, self.bounds.size.height/);

    CGFloat radius = (self.bounds.size.width - _lineWidth)/;

    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 * *(float)M_PI) + startAngle;
    }
    else
    {
        endAngle = (*(float)M_PI) + startAngle;
    }

    [processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [trackPath addArcWithCenter:center radius:radius startAngle: endAngle:*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 = ;
        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(, , , )];
    //设置轨道颜色
    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)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //视图隐藏
//        [view hide];
    });

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果:

IOS贝塞尔曲线圆形进度条和加载动画的更多相关文章

  1. 超酷jQuery进度条加载动画集合

    在丰富多彩的网页世界中,进度条加载动画的形式非常多样,有利用gif图片实现的loading动画,也有利用jQuery和CSS3实现的进度加载动画,本文主要向大家介绍很多jQuery和CSS3实现的进度 ...

  2. 用HTML、CSS、JS制作圆形进度条(无动画效果)

    逻辑 1.首先有一个圆:蓝色的纯净的圆,效果: 2.再来两个半圆,左边一个,右边一个将此蓝色的圆盖住,效果: 此时将右半圆旋转60°,就会漏出底圆,效果:   然后我们再用一个比底圆小的圆去覆盖这个大 ...

  3. CSS3 Loading进度条加载动画特效

    在线演示 本地下载

  4. CSS3彩色进度条加载动画 带进度百分比

    在线演示       本地下载

  5. 基于CAShapeLayer和贝塞尔曲线的圆形进度条动画

    通过CAShapeLayer和贝塞尔曲线搭配的方法,创建的简单的圆形进度条的教程先简单的介绍下CAShapeLayer1,CAShapeLayer继承自CALayer,可使用CALayer的所有属性2 ...

  6. 基于CAShapeLayer和贝塞尔曲线的圆形进度条动画【装载】

    初次接触CAShapeLayer和贝塞尔曲线,看了下极客学院的视频.对初学者来说感觉还不错.今天来说一个通过CAShapeLayer和贝塞尔曲线搭配的方法,创建的简单的圆形进度条的教程先简单的介绍下C ...

  7. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. [iOS]圆形进度条及计时功能

    平时用战网安全令的时候很喜欢圆形倒计时的效果,然后简单看了一下Android的圆形进度条,后来又写了一个IOS的.整体界面参照IOS系统的倒计时功能,顺便熟悉了UIPickerView的一些特性的实现 ...

  9. IOS 圆形进度条

    // // CCProgressView.h // Demo // // Created by leao on 2017/8/7. // Copyright © 2017年 zaodao. All r ...

随机推荐

  1. Python笔记之format()格式输出全解

    格式化输出:format() format():把传统的%替换为{}来实现格式化输出 使用位置参数:就是在字符串中把需要输出的变量值用{}来代替,然后用format()来修改使之成为想要的字符串,位置 ...

  2. .Net面试经验,从北京到杭州

    首先简单说下,本人小本,目前大四软件工程专业,大三阴差阳错地选了.Net方向,也是从大三开始接触.Net.自认为在学生中.net基础还可以,嘿嘿,吹一下. 大四第一学期学校安排去北京培训,培训了两个月 ...

  3. 会HTML/CSS就可以轻松创建网站

    网站其本质就是HTML + CSS 外加一些JavaScript构成的.所以基本上只要你会一些前端,就可以开始花样搭网站了. 如果只用HTML/CSS那做出来的网站只能叫静态网站,性能好但维护不方便, ...

  4. Pi 实现VLC网络视频监控

    安装摄像头 将Pi的原装摄像头安装在pi上面 ssh连接pi 通过xshell或者putty等工具连接ssh 安装VLC $ sudo apt-get update $ sudo apt-get in ...

  5. WPF 组织机构下拉树多选,递归绑定方式现实

    使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Clie ...

  6. Android 如何查看源码 (eclipse 按住 ctrl )

    首先要确认 Android SDK Manager 下载并安装了 sources.然后在代码中按住 ctrl 在点一个类名, 如果打开的页面是找不到源码,就点那个按钮,然后找到源码所在的文件夹,就可以 ...

  7. 【OCP新题库】052最新题库解析-第5题

    5.Which two affect the time taken for instance recovery? A) size of redo logs B) size of UNDO tables ...

  8. Linux下的压缩及归档

    Linux下常用的压缩格式有: gz,bz2,xz,zip,Z //只能压缩文件不能压缩目录,如果传递一个目录,他会把目录中的文件逐个压缩 ..压缩算法:算法不同,压缩比也不同 gz:gzip,压缩后 ...

  9. Mybatis中的@Param注解

    前言 今天在使用Mybatis时遇到一个问题,主要出错原因在于Mybatis的@Param注解,如果我不在参数前面加上@Param注解,数据库操作就会报错,如下: @Param作用 @Param注解的 ...

  10. java的MethodHandle类详解

    一.总述   java7为间接调用方法提供了MethodHandle类,即方法句柄.可以将其看作是反射的另一种方式. 这是使用MethodHandle调用方法的一个例子: public class T ...