IOS custom progress bar and ios custom progress bar

Source: Internet
Author: User

IOS custom progress bar and ios custom progress bar
Custom bar progress bar (iOS)

ViewController. m file

# Import "ViewController. h "@ interface ViewController () @ property (strong, nonatomic) UIButton * btnBegin; @ property (strong, nonatomic) UIButton * btnStop; @ property (strong, nonatomic) UIButton * btnResert; @ property (strong, nonatomic) UITextField * shangx; @ property (strong, nonatomic) UIproessMy * proess; @ end @ implementation ViewController-(void) viewDidLoad {self. proess = [[UIproessMy alloc] initWithFrame: CGRectMake (80,100,250, 20)]; self. proess. backgroundColor = [UIColor clearColor]; [self. view addSubview: self. proess]; // set the value percentage self. proess. index = 80; [self setBtn];} // generate button-(void) setBtn {self. btnBegin = [[UIButton alloc] initWithFrame: CGRectMake (100,500,200, 50)]; self. btnBegin. backgroundColor = [UIColor colorWithRed: 0.294 green: 1.000 blue: 0.368 alpha: 1.000]; self. btnBegin. layer. cornerRadius = 25; self. btnBegin. layer. masksToBounds = YES; [self. btnBegin setTitle: @ "start" forState: UIControlStateNormal]; [self. btnBegin addTarget: self action: @ selector (chang) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. btnBegin]; self. btnStop = [[UIButton alloc] initWithFrame: CGRectMake (100,600,200, 50)]; self. btnStop. backgroundColor = [UIColor colorWithRed: 0.294 green: 1.000 blue: 0.368 alpha: 1.000]; self. btnStop. layer. cornerRadius = 25; self. btnStop. layer. masksToBounds = YES; [self. btnStop setTitle: @ "stop" forState: UIControlStateNormal]; [self. btnStop addTarget: self action: @ selector (stopBtn) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. btnStop]; self. btnResert = [[UIButton alloc] initWithFrame: CGRectMake (100,400,200, 50)]; self. btnResert. backgroundColor = [UIColor colorWithRed: 0.294 green: 1.000 blue: 0.368 alpha: 1.000]; self. btnResert. layer. cornerRadius = 25; self. btnResert. layer. masksToBounds = YES; [self. btnResert setTitle: @ "reset" forState: UIControlStateNormal]; [self. btnResert addTarget: self action: @ selector (Resert) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: self. btnResert];} // stop-(void) stopBtn {[self. proess stop];} // start-(void) chang {[self. proess begin];} // reset-(void) Resert {[self. proess resert];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} @ end

Custom class

UIproessMy. h

#import <UIKit/UIKit.h>@interface UIproessMy : UIView@property(strong,nonatomic)UIView *aView;@property(strong,nonatomic)UIView *UIProess;@property(strong,nonatomic)UILabel *label;@property(strong,nonatomic)NSTimer *time;@property(assign,nonatomic)float index;-(void)change;-(void)begin;-(void)stop;-(void)resert;@end

UIproessMy. m file

# Import "UIproessMy. h "@ implementation UIproessMy-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// initialize self. aView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,250, 20)]; self. aView. backgroundColor = [UIColor colorWithRed: 0.841 green: 0.848 blue: 0.773 alpha: 1.000]; self. UIProess = [[UIView alloc] initWithFrame: CGRectMake (0, 1, 0, 18)]; self. aView. layer. cornerRadius = 10; self. aView. layer. masksToBounds = YES; self. UIProess. layer. cornerRadius = 10; self. UIProess. layer. masksToBounds = YES; self. UIProess. backgroundColor = [UIColor colorWithRed: 0.327 green: 0.816 blue: 0.075 alpha: 1.000]; self. label = [[UILabel alloc] initWithFrame: CGRectMake (0, 50,250,200)]; self. label. backgroundColor = [UIColor colorWithRed: 0.457 green: 0.889 blue: 1.000 alpha: 1.000]; self. label. layer. cornerRadius = 100; self. label. layer. masksToBounds = YES; self. label. textAlignment = NSTextAlignmentCenter; self. label. font = [UIFont boldSystemFontOfSize: 50]; self. label. text = [NSString stringWithFormat: @" 0% % "]; [self. aView addSubview: self. UIProess]; [self addSubview: self. aView]; [self addSubview: self. label];} return self;} // start-(void) begin {if (self. UIProess. frame. size. width <250 * (self. index) * 0.01) {self. time = [NSTimer scheduledTimerWithTimeInterval: 0.01 target: self selector: @ selector (change) userInfo: nil repeats: YES]; [[nsunloop mainRunLoop] addTimer: self. time forMode: NSDefaultRunLoopMode];} else {[self. time invalidate] ;}}// reset-(void) resert {[self initWithFrame: CGRectMake (0, 1, 0, 18)]; self. label. text = [NSString stringWithFormat: @" 0% % "]; [self. time setFireDate: [NSDate distantFuture];} // stop-(void) stop {int a = 1; if (a = 1) {[self. time setFireDate: [NSDate distantFuture]; a = 0;} else {[self. time setFireDate: [NSDate date]; a = 1 ;}// change call timer-(void) change {CGRect changeFrame = self. UIProess. frame; changeFrame. size. width ++ = 0.25; self. UIProess. frame = changeFrame; if (self. UIProess. frame. size. width & gt; = 250 * (self. index) * 0.01) {[self. time invalidate]; // [self initWithFrame: self. UIProess. frame];} self. label. text = [NSString stringWithFormat: @ "%. 1f % ", self. UIProess. frame. size. width/250*100];} @ end

Effect

Related Article

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.