iOS Bezier circular progress bar and load animation

Source: Internet
Author: User

Do the project to do a load animation, a circle in the middle of a picture, there are many online demo, here I also wrote a, the middle of the picture can be added. The main use of Bezier curves. Uibezierpath is a further encapsulation of the cgcontextref, not much to say directly on the code:

#import<UIKit/UIKit.h>@interfaceCircleloader:uiview//Progress Color@property (nonatomic, retain) uicolor*Progresstintcolor;//Track Color@property (nonatomic, retain) uicolor*Tracktintcolor;//Track width@property (nonatomic,assign)floatlinewidth;//Middle Picture@property (nonatomic,strong) UIImage *CenterImage;//Progress@property (nonatomic,assign)floatProgressvalue;//Tip title@property (nonatomic,strong) NSString *Prompttitle;//Turn on animation@property (nonatomic,assign) BOOL animationing;//Hide Disappear- (void) Hide;@end
#import "CircleLoader.h"@interfaceCircleloader () @property (nonatomic,strong) Cashapelayer*Tracklayer, @property (nonatomic,strong) Cashapelayer*Progresslayer;@end@implementationCircleloader-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Self.backgroundcolor=[Uicolor Clearcolor]; }    returnSelf ;}-(void) DrawRect: (CGRect) rect{[Super Drawrect:rect]; _tracklayer=[Cashapelayer layer]; _tracklayer.frame=cgrectmake (0,0, Self.frame.size.width, self.frame.size.height); _tracklayer.linewidth=_linewidth; _tracklayer.strokecolor=_tracktintcolor.cgcolor; _tracklayer.fillcolor=Self.backgroundColor.CGColor; _tracklayer.linecap=Kcalinecapround;        [Self.layer Addsublayer:_tracklayer]; _progresslayer=[Cashapelayer layer]; _progresslayer.frame=cgrectmake (0,0, Self.frame.size.width, self.frame.size.height); _progresslayer.linewidth=_linewidth; _progresslayer.strokecolor=_progresstintcolor.cgcolor; _progresslayer.fillcolor=Self.backgroundColor.CGColor; _progresslayer.linecap=Kcalinecapround;        [Self.layer Addsublayer:_progresslayer]; if(_centerimage!=Nil) {Uiimageview*centerimgview=[[Uiimageview alloc]initwithimage:_centerimage]; Centerimgview.frame=cgrectmake (_linewidth, _linewidth, self.frame.size.width-2*_linewidth, self.frame.size.height-_linewidth*2);//Centerimgview.center=self.center;centerimgview.layer.cornerradius= (self.frame.size.width+_linewidth)/2; Centerimgview.clipstobounds=YES;    [Self.layer AddSublayer:centerImgView.layer]; } [self start];}- (void) Drawbackgroundcircle: (BOOL) animationing {//Bézier curve 0 degrees is in the right direction of the cross-M_PI/2 equivalent to the upper direction of the crossCGFloat startangle =-((float) M_PI/2);//Degrees//CGFloat Endangle = (2* (float) M_PI) +-((float) M_PI/8);; Cgpoint Center= Cgpointmake (self.bounds.size.width/2, self.bounds.size.height/2); CGFloat radius= (Self.bounds.size.width-_linewidth)/2; Uibezierpath*processpath =[Uibezierpath Bezierpath];//Processpath.linewidth=_linewidth;Uibezierpath*trackpath =[Uibezierpath Bezierpath];//Trackpath.linewidth=_linewidth; //---------------------------------------    //Make end angle to 90% of the progress//---------------------------------------    if(!animationing) {Endangle= (_progressvalue *2*(float) M_PI) +startangle; }    Else{Endangle= (0.1*2*(float) M_PI) +startangle;    } [Processpath addarcwithcenter:center Radius:radius startangle:startangle endangle:endangle Clockwise:YES]; [Trackpath addarcwithcenter:center Radius:radius startangle:0Endangle:2*M_pi Clockwise:yes]; _progresslayer.path=Processpath.cgpath; _tracklayer.path=Trackpath.cgpath;}- (void) start{[self drawbackgroundcircle:_animationing]; if(_animationing) {cabasicanimation*rotationanimation = [Cabasicanimation animationwithkeypath:@"transform.rotation.z"]; Rotationanimation.tovalue= [NSNumber Numberwithfloat:m_pi *2.0]; Rotationanimation.duration=1; Rotationanimation.cumulative=YES; Rotationanimation.repeatcount=huge_valf; [_progresslayer addanimation:rotationanimation Forkey:@"rotationanimation"]; }    }- (void) hide{[_progresslayer removeallanimations]; [Self Removefromsuperview];}@end

Call:

#import "ViewController.h"#import "CircleLoader.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //set the view sizeCircleloader *view=[[circleloader Alloc]initwithframe:cgrectmake ( -, -, -, -)]; //Set Track ColorView.tracktintcolor=[Uicolor Redcolor]; //Set the progress bar colorView.progresstintcolor=[Uicolor Greencolor]; //Set Track widthView.linewidth=5.0; //Set ProgressView.progressvalue=0.7; //set whether to go to Yes progress without settingview.animationing=YES; //add an intermediate picture without setting it does not appearView.centerimage=[uiimage imagenamed:@"yzp_loading"]; //Add a View[Self.view Addsubview:view]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5* nsec_per_sec)), Dispatch_get_main_queue (), ^{        //View Hidden//[view hide];    }); }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Effect:

iOS Bezier circular progress bar and load animation

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.