1.动态指示标识(ActivityIndicatorView)
在.h文件中添加一下代码:
@property (retain, nonatomic) UIProgressView *pProgress;
@property (retain, nonatomic) UILabel *pProgressValueLabel;
@property (assign, nonatomic) float progressValue;
在.m文件中实现方法:
- (void)addActivityIndicatorView:(id)sender
{
//创建初始化ActivityIndicatorView(Style)
self.pActivity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//设置pActivity的位置
[self.pActivity setFrame:CGRectMake(10, 280, 45, 45)];
//设置是否隐藏
self.pActivity.hidden = NO;
//把pActivity添加到当前视图中
[self.view addSubview:self.pActivity];
//创建按钮,关联对应方法
UIButton *pBtton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pBtton setFrame:CGRectMake(10, 330, 45,30 )];
[pBtton setTitle:@"转" forState:UIControlStateNormal];
//给按钮关联方法,实现pActivity在不同状态下的动作
[pBtton addTarget:self action:@selector(doRotate:) forControlEvents:UIControlEventTouchUpInside];
//为按钮创建标签,唯一标记,可再次找到
pBtton.tag = 110;
[self.view addSubview:pBtton];
//把创建的对象释放
[pBtton release];
}
//为按钮设置的方法,改变指示标识的运行状态
- (void)doRotate:(id)sender
{
UIButton *pBtton = (UIButton *)sender;
//判断Activity是否运行来设置其运行状态
[self.pActivity isAnimating]?[self.pActivity stopAnimating]:[self.pActivity startAnimating];
//根据Activity是否运行来设置按钮的title
[pBtton setTitle:(self.pActivity.isAnimating?@"停":@"转")forState:UIControlStateNormal];
//设置是否隐藏
self.pActivity.hidden = NO;
}
生成的标识如下图:
- (void)addProgress:(id)sender
{
//创建进度条并初始化
self.pProgress = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 360, 300, 30)];
//设置Style
self.pProgress.progressViewStyle = UIProgressViewStyleBar;
//分别设置进度条完成未完成的颜色
self.pProgress.trackTintColor = [UIColor redColor];
self.pProgress.progressTintColor = [UIColor greenColor];
//把进度条添加到当前的视图中
[self.view addSubview:self.pProgress];
//创建用来显示进度的label
self.pProgressValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 370, 110, 30)];
self.pProgressValueLabel.backgroundColor = [UIColor clearColor];
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加载:%.1f%%",self.progressValue*100];
[self.view addSubview:self.pProgressValueLabel];
//通过计时器完成循环调用
pTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(progressMethod:) userInfo:nil repeats:YES];
}
在.h文件中添加NSTimer * pTimer 成员
//计时器的实现方法
- (void)progressMethod:(id)sender
{
//进度条每次进行的变化
self.progressValue += 0.1;
self.pProgress.progress = self.progressValue;
//把进度条当前的进度显示
self.pProgressValueLabel.text = [NSString stringWithFormat:@"加载:%.1f%%",self.progressValue*100];
//判断进度条是否以完成
if (self.progressValue >= 1)
{
self.progressValue = 0;
//设置标签的名字
self.pProgressValueLabel.text = @"加载完成";
//停止计时器
[pTimer invalidate];
}
}