Implementation ideas
- To implement a drawing, you typically need to customize a UIView subclass, overriding the-(void) DrawRect: (CGRect) Rect method of the parent class, implementing the drawing operation in the method
- To display the download progress, you only need to instantiate the object of the custom subclass (if the control in storyboard, just modify the control's Class property to customize the subclass name)
- The effect shown is actually to draw an arc, dynamically change the position of the end point, and finally reach a closed circle
- The middle text is a Uilabel control that dynamically changes the reality of the text according to the progress
Implementation steps
Customizing a subclass of UIView
//提供一个成员属性,接收下载进度值@property (nonatomicassignCGFloat progress;
Overriding the setter of member properties progress
//每次改变成员属性progress的值,就会调用它的setter- (void)setProgress:(CGFloat)progress{ _progress = progress; //当下载进度改变时,手动调用重绘方法 [self setNeedsDisplay];}
Rewrite-(void) DrawRect: (cgrect) rect ( 核心 )
- (void) DrawRect: (CGRect) rect{//Set radius of Arc CGFloatRadius = rect. Size. Width*0.5;//Set the center of the arc CgpointCenter = cgpointmake (radius, radius);//Set the starting angle of the arc (in radians) CGFloatStartAngle =-m_pi_2;//Set the end angle of the arc CGFloatEndangle =-M_pi_2 +2* M_PI * Self. Progress;//Use the Uibezierpath class to draw arcsUibezierpath *path = [Uibezierpath bezierpathwitharccenter:center Radius:radius-5Startangle:startangle Endangle:endangle Clockwise:YES];//render the drawn arc onto the layer (that is, displayed)[Path stroke];}
The latest status of this blog will be synced to Sina Weibo account: secular island
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iosquartz2d-02-drawing a cool download progress bar