Custom progress bar (cainiao version), custom progress bar cainiao
Below is the code, with some annotations
Create a class first
#import <UIKit/UIKit.h>@interface MYjindutiao : UIView@property(strong,nonatomic)UIView *aview;@property(strong,nonatomic)UIView *bview;@property(strong,nonatomic)UILabel *laber;@property(strong,nonatomic)NSTimer *time;@property(assign,nonatomic)int a;@end
# Import "MYjindutiao. h "@ implementation MYjindutiao-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {self. aview = [[UIView alloc] initWithFrame: CGRectMake (100,100,200, 30)]; self. aview. backgroundColor = [UIColor colorWithRed: 0.082 green: 0.969 blue: 1.000 alpha: 1.000]; // round the corner self. aview. layer. cornerRadius = 10; self. aview. layer. masksToBounds = YES; self. bview = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 0, 30)]; self. bview. backgroundColor = [UIColor colorWithRed: 0.020 green: 1.000 blue: 0.085 alpha: 1.000]; self. bview. layer. cornerRadius = 10; self. bview. layer. masksToBounds = YES; self. laber = [[UILabel alloc] initWithFrame: CGRectMake (100,200,200,200)]; self. laber. backgroundColor = [UIColor colorWithRed: 1.000 green: 0.885 blue: 0.050 alpha: 1.000]; // turns laber into a Spherical self. laber. layer. cornerRadius = 100; self. laber. layer. masksToBounds = YES; // font center self. laber. textAlignment = NSTextAlignmentCenter; // enlarge the self font. laber. font = [UIFont systemFontOfSize: 40 weight: 40]; self. laber. textColor = [UIColor colorWithRed: 0.957 green: 0.950 blue: 1.000 alpha: 1.000]; [self addSubview: self. aview]; [self. aview addSubview: self. bview]; [self addSubview: self. laber]; self. time = [NSTimer scheduledTimerWithTimeInterval: 0.01 target: self selector: @ selector (change) userInfo: nil repeats: YES]; [[nsunloop mainRunLoop] addTimer: self. time forMode: NSDefaultRunLoopMode];} return self;}-(void) change {CGRect chFrame = self. bview. frame; chFrame. size. width ++ = 0.25; self. bview. frame = chFrame; self. laber. text = [NSString stringWithFormat: @ "%. 2f % ", self. bview. frame. size. width/2]; if (self. bview. frame. size. width = self. a) {// clear the timer [self. time invalidate] ;}}@ end
#import <UIKit/UIKit.h>#import "MYjindutiao.h"@interface ViewController : UIViewController@property(strong,nonatomic)MYjindutiao *jindutiao;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.jindutiao=[[MYjindutiao alloc]initWithFrame:CGRectMake(0, 100, 300, 500)]; self.jindutiao.a=160; [self.view addSubview:self.jindutiao]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end