Custom progress bar and android custom progress bar

Source: Internet
Author: User
Tags linecap

Custom progress bar and android custom progress bar

With CAShapeLayer, you can create the desired progress bar shape based on the imported "UIBezierPath * path. Then, you can update the progress of the progress bar by setting the strokeEnd attribute of CAShapeLayer in real time.

Some code:

/** Progress bar and background */-(CAShapeLayer *) createLayer :( UIColor *) strokeColor path :( UIBezierPath *) path {CAShapeLayer * layer = [CAShapeLayer layer]; layer. lineWidth = 5; layer. fillColor = [UIColor clearColor]. CGColor; layer. strokeColor = strokeColor. CGColor; layer. path = path. CGPath; [self. layer addSublayer: layer]; return layer;}/** set progress stroke to stop */-(void) setProgress :( CGFloat) SS {_ progress = progress; self. progressLabel. text = [NSString stringWithFormat: @ "%. 0f % ", progress * 100]; self. progressLayer. strokeEnd = _ progress;}/** create a beiser curve */-(UIBezierPath *) createPathWith :( CGRect) frame {UIBezierPath * path = [UIBezierPath bezierPath]; switch (self. style) {case CusProgressViewStyleNone: {path = [UIBezierPath bezierPathWithArcCenter: CGPointMake (frame. size. width/2.0, frame. size. height/2.0) radius: CGRectGetWidth (frame)/2.0 startAngle:-M_PI_2 endAngle: M_PI_2 * 3 clockwise: YES];} break; case CusProgressViewStyleRectSegment: {CGFloat minAngle = 2 * M_PI/self. gridCount; for (int I = 0; I <self. gridCount; I ++) {CGFloat startAngle = I * minAngle; CGFloat endAngle = (I + 2/3. 0) * minAngle; UIBezierPath * path1 = [UIBezierPath bezierPathWithArcCenter: CGPointMake (frame. size. width/2.0, frame. size. height/2.0) radius: CGRectGetWidth (frame)/2.0 startAngle:-M_PI_2 + startAngle endAngle:-M_PI_2 + endAngle clockwise: YES]; [path appendPath: path1] ;}} break; case CusProgressViewStyleRoundSegment: {self. backgroundLayer. lineCap = kCALineCapRound; self. backgroundLayer. lineJoin = kCALineJoinRound; self. progressLayer. lineCap = kCALineCapRound; self. progressLayer. lineJoin = kCALineJoinRound; static CGFloat minAngle = 1; for (int I = 0; I <ceil (360/(minAngle + self. gridInterval); I ++) {CGFloat startAngle = I * (minAngle + self. gridInterval) * M_PI/180.0; CGFloat endAngle = startAngle + minAngle * M_PI/180.0; if (endAngle> = 2 * M_PI) {endAngle = 2 * M_PI ;} UIBezierPath * path2 = [UIBezierPath bezierPathWithArcCenter: CGPointMake (frame. size. width/2.0, frame. size. height/2.0) radius: CGRectGetWidth (frame)/2.0 startAngle:-M_PI_2 + startAngle endAngle:-M_PI_2 + endAngle clockwise: YES]; [path appendPath: path2] ;}} break; default: break;} return path ;}

 

Exercise code: http://pan.baidu.com/s/1mhxGEuc

Related Article

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.