One: Effect
Two: Code
#import "ViewController.h"#import "ProgressView.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*Valuetitle, @property (weak, nonatomic) Iboutlet Progressview*Progressview;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}-(Ibaction) ValueChange: (UISlider *) Sender {//Get Progress ValueNSLog (@"%f", Sender.value); //% in stringWithFormat has a special meaning and cannot be used directly if you want to use two% to represent a%Self.valueTitle.text = [NSString stringWithFormat:@"%.2f%%", Sender.value * -]; Self.progressView.progressValue=Sender.value; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
1. Build the interface.
2. Drag the slider to allow him to follow my drag, the number is changing.
When the number changes, there is a point of note, that is, to display the%, it is a special symbol, to use two percent of a%
3. When you drag the slider, the arc is drawn on it.
From the top, draw clockwise, so it's starting angle is-90 degrees. The end angle is also-90 degrees
And starting from the starting point,
Starting angle-90 degrees, see what your download progress is
If your download progress is 100, that's 1 * 360 degrees.
Which means the progress is one of your 360 degrees.
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Cgpoint Center = cgpointmake (50, 50);
CGFloat radius = rect.size.width * 0.5;
CGFloat Starta =-m_pi_2;
CGFloat enda =-m_pi_2 + M_PI * 2 * progress;
Uibezierpath *path = [Uibezierpath bezierpathwitharccenter:center
Radius:radius
Startangle:starta
Endangle:enda
Clockwise:yes];
To get the value of the progress, this progress value is not, so it must be passed in to draw. Get a member variable
It is necessary to pass in when the value changes.
To get Progressview to be able to pass in, so to drag line, get Progressview
All done well, found not to draw a circle of loneliness?
Why?
Question: How many times does the DrawRect method call total?
A total of one call, the first progress is 0, will not be executed in the future
Resolution: Every time you pass, you need to draw it once,
Overriding the Progress method
-(void) Setprogress: (cgfloat) progress{
_progress = progress;
Call the DrawRect method manually, and let it redraw
[Self drawRect:self.bounds];
}
Run the discovery or not draw, why?
Cause: The DrawRect method cannot be called manually because the context associated with the view can be obtained in the DrawRect method
When you call the DrawRect method, the system automatically creates a context that is associated with the view and passes it to it.
Called by itself, without giving the context to the DrawRect method. Therefore, the context is not taken in the draw method.
Workaround: To redraw, invoke [self setneedsdisplay];
Tells the system to redraw the view, the system will automatically call the DrawRect method, the system calls the
DrawRect method that will help you create the context
iOS Development quartz2d Application of three progress bar