活動(dòng)指示器
活動(dòng)指示器可以消除用戶的心理等待時(shí)間,并且如果我們不知道什么時(shí)候結(jié)束任務(wù)就可以使用活動(dòng)指示器。
活動(dòng)指示器的屬性中style有3個(gè)冬三,分別為L(zhǎng)arge White、White缘缚、Gray勾笆。
Behavior屬性中Animating被選中后,當(dāng)運(yùn)行時(shí)控件會(huì)處于活動(dòng)狀態(tài)桥滨;Hides When Stopped被選中后窝爪,當(dāng)控件處于非活動(dòng)狀態(tài)的時(shí)候,控件會(huì)被隱藏齐媒。
下面做個(gè)小的Demo:(界面布局如圖所示)
Paste_Image.png
代碼如下所示:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;
- (IBAction)startToMove:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)startToMove:(id)sender {
//isAnimating用于判斷活動(dòng)指示器是否處于活動(dòng)狀態(tài)
if ([self.activityIndicatorView isAnimating]) {
[self.activityIndicatorView stopAnimating];
} else {
[self.activityIndicatorView startAnimating];
}
}
@end
進(jìn)度條
進(jìn)度條同樣也可以消除用戶的心理等待時(shí)間蒲每。在下面這個(gè)Demo中,為了模擬真實(shí)的任務(wù)變化喻括,使用定時(shí)器邀杏。
界面如下:
Paste_Image.png
代碼如下:
@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