首页 > 其他 > 详细

CAShapeLayer 结合 UIBezierPath 画圆

时间:2015-10-14 16:06:30      阅读:394      评论:0      收藏:0      [点我收藏+]

  

  CAShapeLayer 是什么 ? (待更新)

 

  UIBezierPath 是什么 ? (待更新)

 

  实例:

  #import <UIKit/UIKit.h>


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

@property (nonatomic, strong) UIColor *trackColor;
@property (nonatomic, strong) UIColor *progressColor;
@property (nonatomic) float progress;//0~1之间的数
@property (nonatomic) float progressWidth;

- (void)setProgress:(float)progress animated:(BOOL)animated;

@end

 

 #import "QACircleProgressView.h"


@implementation QACircleProgressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // Initialization code
        _trackLayer = [CAShapeLayer new];
        _trackLayer.fillColor = nil;
        _trackLayer.frame = self.bounds;
        [self.layer addSublayer:_trackLayer];
        
        _progressLayer = [CAShapeLayer new];
        [self.layer addSublayer:_progressLayer];
        _progressLayer.fillColor = nil;
        _progressLayer.lineCap = kCALineCapRound;
        _progressLayer.frame = self.bounds;
        
        //默认5
        self.progressWidth = 5;
    }
    return self;
}

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

- (void)setProgress
{
    _progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2) radius:self.bounds.size.width/ 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];
}

- (void)setProgress:(float)progress animated:(BOOL)animated
{
    
}

@end

 

 

  

  ViewController:

  /**

 *  使用CAShapeLayer 结合 UIBezierPath 画出想要的图形
 *  1,新建UIBezierPath 对象
 *  2,新建CAShapeLayer 对象 
 *  3,bezierPath 对象的CGPath 赋值给caShapeLayer.path
 *  4,把caShapeLayer添加到某个图形的layer中
 
*/
- (void)viewDidLoad {
    [super viewDidLoad];
  
    QACircleProgressView *progressView = [[QACircleProgressView alloc]initWithFrame:CGRectMake(20,508080)];
     [self.view addSubview:progressView];
    progressView.progressWidth = 3;
    progressView.progressColor = [UIColor colorWithRed:136/255.0 green:199/255.0 blue:80/255.0 alpha:1.0];
    progressView.trackColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0];
    progressView.progress = 0.8;
}

   

 运行效果:

 技术分享

 

CAShapeLayer 结合 UIBezierPath 画圆

原文:http://www.cnblogs.com/qiugaoying/p/4877502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!