/**
* 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