UIBezierPath 画圆

 

.h 文件

//
//  MaricoCircleProgressView.h
//  sb
//
//  Created by Augussun on 16/4/28.
//  Copyright © 2016年 QL. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface MaricoCircleProgressView : UIView
{
    CAShapeLayer * _trackLayer;
    UIBezierPath * _trackPath;
    CAShapeLayer * _progressLayer;
    UIBezierPath * _progressPath;
}

/// 底层圆的颜色
@property (nonatomic, strong) UIColor * trackColor;
/// 上层圆的颜色
@property (nonatomic, strong) UIColor * progressColor;
/// 进度 0~1 之间的数
@property (nonatomic, assign) float progress;
/// 两个圆的宽度
@property (nonatomic, assign) float progressWidth;

@end

.m文件

//
//  MaricoCircleProgressView.m
//  sb
//
//  Created by Marico on 16/4/28.
//  Copyright © 2016年 Marico. All rights reserved.
//

#import "MaricoCircleProgressView.h"

@implementation MaricoCircleProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor purpleColor];
        _trackLayer = [[CAShapeLayer alloc]init];
        [self.layer addSublayer:_trackLayer];
        _trackLayer.fillColor = nil;
        _trackLayer.frame = self.bounds;
        
        _progressLayer = [[CAShapeLayer alloc]init];
        [self.layer addSublayer:_progressLayer];
        _progressLayer.fillColor = nil;
        _progressLayer.lineCap = kCALineCapRound;
        _progressLayer.frame = self.bounds;
        
        self.progress = 4.f;
    }
    return self;
}


- (void)setTrack
{
    _trackPath = [UIBezierPath bezierPathWithArcCenter:self.center
                                                radius:(self.bounds.size.width - _progressWidth)/2
                                            startAngle:0
                                              endAngle:M_PI*2
                                             clockwise:YES];
    _trackLayer.path = _trackPath.CGPath;
}

- (void)setProgress
{
    _progressPath = [UIBezierPath bezierPathWithArcCenter:self.center
                                                   radius:(self.bounds.size.width - _progressWidth)/2
                                               startAngle:-M_PI_2 endAngle:(M_PI * 2)* _progress - M_PI_2
                                                clockwise:YES];
    _progressLayer.path = _progressPath.CGPath;
}

- (void)setProgressWidth:(float)progressWidth
{
    _progressWidth = progressWidth;
    _trackLayer.lineWidth = _progressWidth;
    _progressLayer.lineWidth = _progressWidth;
    
    [self setTrack];
    
    [self setProgress];
}


- (void)setTrackColor:(UIColor *)trackColor
{
    _trackLayer.strokeColor = trackColor.CGColor;
}

- (void)setProgressColor:(UIColor *)progressColor
{
    _progressLayer.strokeColor = progressColor.CGColor;
}

- (void)setProgress:(float)progress
{
    _progress = progress;
    
    [self setProgress];
}
@end

 

转载于:https://www.cnblogs.com/caicaige/p/5443529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值