UIBezierPath和CAShapeLayer 简单画圆

//

//  WSQCustomProgressView.h

//  Demo

//

//  Created by webapps on 17/3/6.

//  Copyright © 2017 Passion. All rights reserved.

//



#import <UIKit/UIKit.h>



@interface WSQCustomProgressView : UIView


@property(nonatomic, assign) CGFloat circleWidth;//圆宽

@property(nonatomic,strongUIColor * backCircleClor;//背景圆环的颜色

@property(nonatomic,strongUIColor * progresCircleClor;//进度条圆环的颜色

@property(nonatomic,assignBOOL hidePercentL;//是否隐藏百分比Lable

@property(nonatomic,strongUIColor * percentLClor;//百分比Lable的颜色


// 开始创建Circle

- (void)createCricle;

//开始绘制

-(void)stareAnimationWithPercentage:(CGFloat )percent;

@end











//

//  WSQCustomProgressView.m

//  Demo

//

//  Created by webapps on 17/3/6.

//  Copyright © 2017 Passion. All rights reserved.

//


/*    思路

 

   View 加两个Layer 一个为底部的灰色,一个为深色,控制只有绘制图的时间

 

 

 */







#import "WSQCustomProgressView.h"

#define RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]


@interface WSQCustomProgressView()


@property (nonatomic, strong) CAShapeLayer *backgroundLayer;//背景圆

@property (nonatomic, strong) CAShapeLayer *progressLayer;//进度圆

@property (nonatomic, strong) UILabel      *percentLable;//百分比按钮


@end



@implementation WSQCustomProgressView


- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        

        //初始化view

        self.backgroundColor = [UIColor clearColor];

        

        //默认圆宽度4

        _circleWidth = 4;

        //默认背景圆环为淡淡灰色

        _backCircleClor = RGB(240, 240, 240, 1);

        //默认进度圆环为橙色

        _progresCircleClor = [UIColor orangeColor];

        //默认显示百分比Lable

        _hidePercentL = NO;

        //默认显示百分比字体颜色我淡黑色

        _percentLClor = RGB(212, 212, 213, 1);

        

    }

 

    return self;

}



//开始创建圆环

- (void)createCricle{

    

    

    CGFloat radius = (CGRectGetWidth(self.bounds) - _circleWidth)/2;

    

    //灰色背景圆环

    _backgroundLayer = [self createLayerWithFillColor:[UIColor clearColor] StrokeColor:_backCircleClor lineWidth:_circleWidth radius:radius];

    //显示进度的圆环

    _progressLayer = [self createLayerWithFillColor:[UIColor clearColor] StrokeColor:_progresCircleClor lineWidth:_circleWidth radius:radius];

    _progressLayer.strokeEnd = 0;

    

  

    [self.layer addSublayer:_backgroundLayer];

    [self.layer addSublayer:_progressLayer];

    

    [self createPercentLable];


    

  

    

}




//创建百分比Lable

- (void)createPercentLable{


    CGFloat w = self.bounds.size.width;

    CGFloat H = self.bounds.size.height/2;

    

    _percentLable = [[UILabel alloc]initWithFrame:CGRectMake(0, H - 15, w, 30)];

    _percentLable.hidden = _hidePercentL;

    _percentLable.textColor = _percentLClor;

    _percentLable.textAlignment = NSTextAlignmentCenter;

    _percentLable.backgroundColor = [UIColor clearColor];

    [self addSubview:_percentLable];

}


/**

 对圆环做处理


 @param fillColor 圆环的内部填充颜色

 @param strokeColor 圆环圆填充的颜色

 @param linewidth 圆环宽度

 @param radius 角度

 @return 返回layer

 */

- (CAShapeLayer *)createLayerWithFillColor:(UIColor *)fillColor StrokeColor:(UIColor *)strokeColor lineWidth:(CGFloat)linewidth radius:(CGFloat)radius{

        

    CAShapeLayer *layer = [CAShapeLayer layer];

    layer.bounds = self.bounds;

    layer.anchorPoint = CGPointMake(0, 0);

    layer.lineWidth = linewidth;

    layer.fillColor = fillColor.CGColor;

    layer.strokeColor = strokeColor.CGColor;

    

    // 1/4 pi处起绘制

    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)) radius:radius startAngle:M_PI_2 endAngle:M_PI*2 + M_PI_2 clockwise:YES];

    

    layer.path = bPath.CGPath;

    

    return layer;

        

    }



/**

  开始执行绘制


 @param percent 百分比

 */

- (void)stareAnimationWithPercentage:(CGFloat)percent{

    

    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    basic.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    basic.duration = 5;//绘制时间

    basic.fromValue = @(0);//起始,

    basic.toValue = @(percent);//结束

    basic.removedOnCompletion = NO;//动画执行完成后不删除动画

    basic.fillMode = @"forwards";

    [_progressLayer addAnimation:basic forKey:nil];

    

    int prer = (int)(percent *100);

    _percentLable.text = [NSString stringWithFormat:@"%d%@",prer,@"%"];

    

}


@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值