#import<UIKit/UIKit.h>
@interface ViewController : UIViewController
/**
* 进度条的底层视图
*/
@property(strong,nonatomic) UIView *myview;
/**
* 进度条的变化视图
*/
@property(strong,nonatomic) UIView *Aview;
/**
* 显示进度值
*/
@property(strong,nonatomic) UILabel *lbltext;
/**
* 控制进度条的进度
*/
@property(strong,nonatomic) NSTimer *timer;
/**
* 暂无
*/
@property(strong,nonatomic) UITextField *textfield;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
static float sec = 0.0;
- (void)viewDidLoad {
[super viewDidLoad];
//初始化视图
self.Aview = [[UIView alloc] initWithFrame:CGRectMake(50, 200, 300, 30)];
//设置视图的背景颜色
self.Aview.backgroundColor = [UIColor brownColor];
//对视图进行倒圆角
self.Aview.layer.cornerRadius = 10;
//添加视图到父视图
[self.view addSubview:self.Aview];
//初始化视图
self.myview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];
//设置视图 的背景颜色
self.myview.backgroundColor = [UIColor blueColor];
//对视图的宽度进行倒圆角
self.myview.layer.cornerRadius = 10;
//添加 myview视图 到 Aview视图上
[self.Aview addSubview:self.myview];
self.textfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 240, 200, 100)];
self.textfield.backgroundColor = [UIColor greenColor];
self.textfield.layer.cornerRadius = 50;
self.textfield.textAlignment = NSTextAlignmentCenter;
self.textfield.font = [UIFont boldSystemFontOfSize:25];
self.textfield.keyboardType = UIKeyboardTypeNumberPad;
self.textfield.placeholder = @"请输入0~100的数";
[self.view addSubview:self.textfield];
//初始化标签
self.lbltext = [[UILabel alloc] initWithFrame:CGRectMake(50, 170, 200, 30)];
//添加标签文字
self.lbltext.text = @"进度条";
//添加标签的背景颜色
self.lbltext.backgroundColor = [UIColor orangeColor];
//设置标签的字体大小
self.lbltext.font = [UIFont boldSystemFontOfSize:30];
//将字体进行居中书写
self.lbltext.textAlignment = NSTextAlignmentCenter;
//添加到父视图
[self.view addSubview:self.lbltext];
//添加定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(handl) userInfo:nil repeats:YES];
}
//触发事件
-(void)handl{
//获取 myview 视图 的bounds
CGRect rect = self.myview.bounds;
//在标签上显示进度值
self.lbltext.text = [NSString stringWithFormat:@"进度:%.2f%%",sec];
//获取 myview视图 的 宽度
rect.size.width = sec*3;
//给myview视图重新设置 尺寸
self.myview.frame = rect;
//设置定时器在多少的时间内停止
if (sec > 14) {
//使定时器无效
[self.timer invalidate];
//给定时器设置为空
self.timer = nil;
}
//设置 sec 的步进
sec +=0.1;
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
if ([self.textfield isFirstResponder]) {
[self.textfield resignFirstResponder];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end