Ios-calayer Implementing a simple progress bar

Source: Internet
Author: User

/**
* Custom Download progress bar control with Calayer
* 1. Create a separate Calayer
* 2. Directly modify the frame value of the Calayer, perform an implicit animation, achieve the progress bar effect
* 3. Use the timer (Nstimer) to simulate the percentage data that is provided when the network is downloaded
* 4. Encapsulating calayer into UIView subclasses customizing the progress bar control

*/

Customize a UIView, create a Calayer

#import<UIKit/UIKit.h>

@interfaceWjprogressview:uiview

@property (Nonatomic,assign) cgfloat progress;//Progress parameter value range 0~100
@property (Nonatomic,strong) Uicolor *progresscolor;//Color

@end#import"WJProgressView.h"

@interfaceWjprogressview ()

@property (Nonatomic,strong) Calayer *progresslayer;
@property (nonatomic,assign) cgfloat currentviewwidth;

@end

@implementationWjprogressview

-(Instancetype) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if(self) {
Self.progresslayer = [Calayer layer];
Self.backgroundcolor = [Uicolor Graycolor];
Self.progressLayer.backgroundColor = [Uicolor Redcolor]. Cgcolor;
Self.progressLayer.frame = CGRectMake (0,0,0, frame.size.height);
[Self.layer AddSublayer:self.progressLayer];
//stores the width value of the current view
Self.currentviewwidth = Frame.size.width;
}
returnSelf
}

#pragmaMark-Overriding the Setter,getter method

@synthesizeprogress = _progress;
- (void) Setprogress: (cgfloat) Progress {
_progress = progress;
if(Progress <=0) {
Self.progressLayer.frame = CGRectMake (0,0,0, self.frame.size.height);
}Elseif(Progress <=1) {
Self.progressLayer.frame = CGRectMake (0,0, Progress *self.currentviewwidth, self.frame.size.height);
}Else{
Self.progressLayer.frame = CGRectMake (0,0, Self.currentviewwidth, self.frame.size.height);
}
}

-(CGFloat) Progress {
return_progress;
}

@synthesizeProgresscolor = _progresscolor;
- (void) Setprogresscolor: (Uicolor *) Progresscolor {
_progresscolor = Progresscolor;
Self.progressLayer.backgroundColor = Progresscolor.cgcolor;
}

-(Uicolor *) Progresscolor {
return_progresscolor;
}

If encapsulated as above, simply change the progress progress property to change the Calayer frame (implicit animation)

Self.progressView.progress = arc4random ()% -/ -. F;

:

Ios-calayer implement a simple progress bar

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.