#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