ActivityIndicatorView可以消除用户的心理等待时间,ProgressView可以指示请求的进度,也有消除用户心理等待时间的作用。
其中在ActivityIndicatorView中,重要的方法:isAnimating方法用于判断ActivityIndicatorView是否处于运动状态,stopAnimating方法用于停止旋转,startAnimating方法用于开始旋转。
进度条中用到了定时器NSTimer的方法scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(bool)repeats,其中second参数设置时间间隔,target用于指定发送消息给那个对象,aSelector指定要调用的方法名,相当于一个函数指针,userInfo可以给消息发送参数,repeats表示是否重复。
实例代码如下:
//
// ViewController.h
// 1006ActivityIndicatorViewAndProgressViewSample
//
// Created by weibiao on 15-10-6.
// Copyright (c) 2015年 weibiao. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSTimer *myTimer;
}
//活动只是其的输出口
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
//活动指示器的方法
- (IBAction)upload:(id)sender;
//进度条的输出口
@property (weak, nonatomic) IBOutlet UIProgressView *myProgressView;
//进度条的方法
- (IBAction)download:(id)sender;
@end
//
// ViewController.m
// 1006ActivityIndicatorViewAndProgressViewSample
//
// Created by weibiao on 15-10-6.
// Copyright (c) 2015年 weibiao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIAlertViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)upload:(id)sender {
if ([self.activityIndicator isAnimating]) {
[self.activityIndicator stopAnimating];
} else {
[self.activityIndicator startAnimating];
}
}
- (IBAction)download:(id)sender {
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(download) userInfo:nil repeats:YES];
}
- (void)download {
self.myProgressView.progress = self.myProgressView.progress + 0.1;
if (self.myProgressView.progress == 1.0) {
[myTimer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"download complete" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
@end