您的位置:首页 > 产品设计 > UI/UE

iOS中的UIProgressView(进度条)

2016-04-14 00:00 429 查看
UIProgressView 进度条

#import "ViewController.h"

@interface ViewController (){
UIProgressView *myProgressView;
UILabel *myLabel;
NSTimer *myTimer;
BOOL yesOrNo;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//新建一个UIProgressView,并且设置位置和大小
myProgressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];

//设置UIProgressView的初始状态(也可以说是起点),满格是1
myProgressView.progress = 0.4;

//设置UIProgressView的线条颜色(需要转到3D图,在侧面才可以看见)
myProgressView.backgroundColor = [UIColor redColor];

//设置进度条的颜色
myProgressView.tintColor = [UIColor blueColor];

//不能直接设置UIProgressView的宽度,但是可以缩放ლ(°◕‵ƹ′◕ლ)
myProgressView.transform = CGAffineTransformScale(myProgressView.transform, 1, 2);

//将UIProgressView添加到手机界面上
[self.view addSubview:myProgressView];

//新建一个Label,来显示进度条的百分比
myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 200, 200)];
[self.view addSubview:myLabel];

//添加一个定时器,每次的速度是0.01,并且添加方法,每0.1秒触发一次
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(haha:) userInfo:nil repeats:YES];

}

////定时器的方法
-(void)haha:(id)a{

//如果进度条的状态没有跑满(1.0)
if (myProgressView.progress<1.0) {
//则进度条就加0.01
myProgressView.progress = myProgressView.progress+0.01;

//myLabel的文字用来显示进度条的百分比,默认是从0.01计算的,所以乘以100
myLabel.text = [NSString stringWithFormat:@"%.0f%%", myProgressView.progress*100];
}else{
//反之如果跑满了,myLabel文字就变成了“安装成功”
myLabel.text = @"安装成功";
[myTimer invalidate];
}

}

//定时器往返的方法
-(void)haha:(id)a{
//设置一个BOOL判断,此处如果是状态yes
if (yesOrNo) {
//则进度条就加0.01
myProgressView.progress = myProgressView.progress+0.01;
//如果跑满了,则状态就变为no
if (myProgressView.progress == 1) {
yesOrNo = !yesOrNo;
}
//如果状态为no
}else if (!yesOrNo){
//因为前面进度条跑满了,状态变为no,所以此处让进度条-0.01
myProgressView.progress = myProgressView.progress-0.01;
//如果减到了0,则状态右变成了yes,一直在循环
if (myProgressView.progress == 0) {
yesOrNo = !yesOrNo;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: