UIActivityIndicatorView和UIProgressView都继承自UIView,所以他们可以附属在其他视图上。UIActivityIndicatorView是一个进度提示器,显示一个小圆圈在转动,主要用在一些耗时操作的提示上,比如网络请求;UIProgressView是一个进度提示条,不过它可以显示一个进度,可以告知用户操作已经进行了多少,这二者的目的都是为了在应用程序有耗时操作时在UI上进行显示,提高用户体验。
下面分别来创建这个两个控件:
首先在ViewController.h中声明代码:
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController
- @property(retain,nonatomic) UIActivityIndicatorView *activityIndicator;
- @property(retain,nonatomic) UIProgressView *progressView;
- - (IBAction)startIndicator:(id)sender;
- - (IBAction)startProgress:(id)sender;
- - (IBAction)startNetWork:(id)sender;
- @end
然后修改ViewController.m文件,具体的解释都在注释里面
- #import "ViewController.h"
- @interfaceViewController ()
- @end
- @implementation ViewController
- @synthesize activityIndicator = _activityIndicator;
- @synthesize progressView = _progressView;
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- }
- - (IBAction)startIndicator:(id)sender {
- //初始化指示器
- self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 200, 30, 30)];
- /*
- 指定指示器的类型
- 一共有三种类型:
- UIActivityIndicatorViewStyleWhiteLarge //大型白色指示器
- UIActivityIndicatorViewStyleWhite //标准尺寸白色指示器
- UIActivityIndicatorViewStyleGray //灰色指示器,用于白色背景
- */
- self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
- //停止后是否隐藏(默认为YES)
- self.activityIndicator.hidesWhenStopped = YES;
- //将Indicator添加到视图中
- [self.viewaddSubview:self.activityIndicator];
- //开始转动
- [self.activityIndicator startAnimating];
- //操作队列
- NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
- //设置最大的操作数
- [operationQueue setMaxConcurrentOperationCount:1];
- //构建一个操作对象,selector指定的方法是在另外一个线程中运行的
- NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
- selector:@selector(runIndicator) object:nil];
- //将操作加入队列,此时后台线程开始执行
- [operationQueue addOperation:operation];
- }
- - (IBAction)startProgress:(id)sender {
- self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(70, 260, 180, 20)];
- /*
- 设置风格属性
- 有两种风格属性:
- UIProgressViewStyleDefault
- UIProgressViewStyleBar
- */
- self.progressView.progressViewStyle = UIProgressViewStyleDefault;
- //设置进度,值为0——1.0的浮点数
- // self.progressView.progress = .5;
- [self.viewaddSubview:self.progressView];
- //设定计时器,每隔1s调用一次runProgress方法
- [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(runProgress) userInfo:nil repeats:YES];
- }
- //在状态栏显示有网络请求的提示器
- - (IBAction)startNetWork:(id)sender {
- UIApplication *app = [UIApplication sharedApplication];
- if (app.isNetworkActivityIndicatorVisible) {
- app.networkActivityIndicatorVisible = NO;
- }else {
- app.networkActivityIndicatorVisible = YES;
- }
- }
- -(void)runIndicator
- {
- //开启线程并睡眠三秒钟
- [NSThread sleepForTimeInterval:3];
- //停止UIActivityIndicatorView
- [self.activityIndicator stopAnimating];
- }
- //增加progressView的进度
- -(void)runProgress
- {
- self.progressView.progress += .1;
- }
- - (void)viewDidUnload
- {
- [superviewDidUnload];
- // Release any retained subviews of the main view.
- [self.activityIndicator release];
- [self.progressView release];
- }
- @end
编译运行后效果如下: