A circular progress bar is achieved through the busel curve and the strokeStart and strokeEnd attributes of CAShapeLayer.

Source: Internet
Author: User

A circular progress bar is achieved through the busel curve and the strokeStart and strokeEnd attributes of CAShapeLayer.

# Import <UIKit/UIKit. h> @ interface CircleProgressView: UIView/** start value (0-1) */@ property (nonatomic, assign) CGFloat fstartValue; /** Border Width */@ property (nonatomic, assign) CGFloat flineWidth;/** line color */@ property (nonatomic, strong) UIColor * lineColor; /** variable value */@ property (nonatomic, assign) CGFloat fvalue; @ end # import "CircleProgressView. h "@ interface CircleProgressView () {Pipeline * _ shapeLayer;} @ end @ implementation CircleProgressView @ synthesize fstartValue = _ fstartValue; @ synthesize flineWidth = _ flineWidth; @ synthesize lineColor = _ lineColor; @ synthesize fvalue = _ fvalue;-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {/** create a layer with shapes */_ shapeLayer = [CAShapeLayer layer]; _ shapeLayer. frame = self. bounds; _ shapeLayer. strokeEnd = 0.f;/* Create a busel curve */UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect: self. bounds];/** associate a layer with a non-Sel curve using a path */_ shapeLayer. path = path. CGPath;/** set the fill color, width, and border color of the layer */_ shapeLayer. fillColor = [UIColor clearColor]. CGColor; _ shapeLayer. lineWidth = 1.0f; _ shapeLayer. strokeColor = [UIColor redColor]. CGColor; [self. layer addSublayer: _ shapeLayer];} return self ;} /*** @ brief override the setter method of fstartValue * @ param fstartValue set the starting value of the circular strokeStart * @ since */-(void) setFstartValue :( CGFloat) fstartValue {_ fstartValue = fstartValue; _ shapeLayer. strokeStart = fstartValue;}-(CGFloat) fstartValue {return _ fstartValue ;} /*** @ brief override flineWidth setter Method * @ param flineWidth set the width of the circular border * @ since */-(void) setFlineWidth :( CGFloat) flineWidth {_ flineWidth = flineWidth; _ shapeLayer. lineWidth = flineWidth;}/*** @ brief override lineColor's setter Method * @ param lineColor sets the circular border color * @ since */-(void) setLineColor :( UIColor *) lineColor {_ lineColor = lineColor; _ shapeLayer. strokeColor = lineColor. CGColor;}-(UIColor *) lineColor {return _ lineColor ;} /*** @ brief override fvalue setter Method * @ param lineColor set the strokeEnd value of the circle * @ since */-(void) setFvalue :( CGFloat) fvalue {_ fvalue = fvalue; _ shapeLayer. strokeEnd = fvalue;}-(CGFloat) fvalue {return _ fvalue;} @ end # import "ViewController. h "# import" CircleProgressView. h "@ interface ViewController () {CircleProgressView * progress;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; progress = [[CircleProgressView alloc] initWithFrame: CGRectMake (0, 0,200,200)]; progress. center = self. view. center; progress. lineColor = [UIColor redColor]; progress. flineWidth = 1.0f; progress. fstartValue = 0; [self. view addSubview: progress]; [NSTimer scheduledTimerWithTimeInterval: 1.0f target: self selector: @ selector (circleAnimation) userInfo: nil repeats: YES] ;}( void) circleAnimation {progress. fvalue = arc4random () %100/100. f;}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} @ end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.