活动指示器
活动指示器可以消除用户的心理等待时间,并且如果我们不知道什么时候结束任务就可以使用活动指示器。
活动指示器的属性中style有3个,分别为Large White、White、Gray。
Behavior属性中Animating被选中后,当运行时控件会处于活动状态;Hides When Stopped被选中后,当控件处于非活动状态的时候,控件会被隐藏。
下面做个小的Demo:(界面布局如图所示)
代码如下所示:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
- (IBAction)startToMove:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)startToMove:(id)sender {
//isAnimating用于判断活动指示器是否处于活动状态
if ([self.activityIndicatorView isAnimating]) {
[self.activityIndicatorView stopAnimating];
} else {
[self.activityIndicatorView startAnimating];
}
}
@end
进度条
进度条同样也可以消除用户的心理等待时间。在下面这个Demo中,为了模拟真实的任务变化,使用定时器。
界面如下:
代码如下:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
- (IBAction)startToMove:(id)sender;
@property (weak, nonatomic) IBOutlet UIProgressView *profressView;
- (IBAction)downloadProgress:(id)sender;
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)startToMove:(id)sender {
if ([self.activityIndicatorView isAnimating]) {
[self.activityIndicatorView stopAnimating];
} else {
[self.activityIndicatorView startAnimating];
}
}
- (IBAction)downloadProgress:(id)sender {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(download)
userInfo:nil
repeats:YES];
}
- (void)download{
self.profressView.progress = self.profressView.progress + 0.1;
if (self.profressView.progress == 1.0) {
[self.timer invalidate];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"下载完成"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertAciton = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:alertAciton];
[self presentViewController:alertController animated:YES completion:nil];
}
}
@end