:
Steps:
1. Drag a controller into the storyboard add UISlider and UIView 2 controls
2. Monitor the value change event, Hjprogressview property of the UISlider in the controller, and pass the changed value to the custom UIView
3. Custom Hjprogressview Override the Progressvalue property set method to redraw the text and Radian values in the view
Controller code:
#import "ViewController.h" #import "HJProgressView.h" @interface Viewcontroller () @property (Nonatomic,strong) Iboutlet Hjprogressview *progressview;-(ibaction) Didslidervaluechange: (UISlider *) sender; @end @implementation viewcontroller-(void) viewdidload { [super viewdidload]; Do any additional setup after loading the view, typically from a nib.} -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} /** * sender.value value 0.0~1.0 * * @param sender < #sender description#> */-(ibaction) Didslidervaluechange: ( UISlider *) Sender { NSLog (@ "%f", sender.value); Self.progressView.progressValue = Sender.value;} @end
Custom View Code:
hjprogressview.m//Draw progress bar////Created by Hjiang on 15/1/2.//Copyright (c) 2015 Hjiang. All rights reserved.//#import "HJProgressView.h" @interface Hjprogressview ()//define properties to save the last Radian start//@property (nonatomic, Assign) CGFloat Endangle, @end @implementation hjprogressview-(void) Setprogressvalue: (float) progressvalue{_ Progressvalue = Progressvalue; Redraw Progressview graphics [self setneedsdisplay];} /** * Draw Progress bar * x, y Center * radius radius * StartAngle The starting position of the arc * Endangel arc End position * Clockwise 0 Shun pin 1 counterclockwise * @param rect rect Description */-(void) DrawRect: (cgrect) rect{//1. Set Progress text cgfloat w = 40; CGFloat h = 30; CGFloat x = 13; CGFloat y = rect.size.height/2-5;//nsstring *progresstext = [NSString stringwithformat:@ "%.02f", Self.progressValue]; Output progress percentage number nsmutablestring *progresstext = [nsmutablestring string]; [Progresstext appendformat:@ "%.01f", Self.progressvalue * 100]; [Progresstext appendstring:@ "%"]; Control text font size and color properties Nsdictionary *attrs = @{nsfontattributename:[uifont Systemfontofsize:10],nsforegroundcolorattributename:[uicolor RedColor]}; [Progresstext drawinrect:cgrectmake (x, Y, W, h) withattributes:attrs]; Cgcontextref CTX = Uigraphicsgetcurrentcontext (); Set the color Cgcontextsetrgbstrokecolor (CTX, 0.5, 0.5, 0.5, 1); Set the line size Cgcontextsetlinewidth (CTX, 3); 2. Draw the arc progress cgfloat endangle = Self.progressvalue * 2 * M_PI-M_PI_4; Cgcontextaddarc (CTX, 25,-m_pi_4, Endangle, 0); Cgcontextstrokepath (CTX); } @end
IOS quartz2d Simulation Download progress bar