UIActivityIndicatorView和UIProgressView都继承自UIView,所以他们可以附属在其他视图上。UIActivityIndicatorView是一个进度提示器,显示一个小圆圈在转动,主要用在一些耗时操作的提示上,比如网络请求;UIProgressView是一个进度提示条,不过它可以显示一个进度,可以告知用户操作已经进行了多少,这二者的目的都是为了在应用程序有耗时操作时在UI上进行显示,提高用户体验。

下面分别来创建这个两个控件:

首先在ViewController.h中声明代码:

  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController
  3. @property(retain,nonatomic) UIActivityIndicatorView *activityIndicator;
  4. @property(retain,nonatomic) UIProgressView *progressView;
  5. - (IBAction)startIndicator:(id)sender;
  6. - (IBAction)startProgress:(id)sender;
  7. - (IBAction)startNetWork:(id)sender;
  8. @end

然后修改ViewController.m文件,具体的解释都在注释里面

  1. #import "ViewController.h"
  2. @interfaceViewController ()
  3. @end
  4. @implementation ViewController
  5. @synthesize activityIndicator = _activityIndicator;
  6. @synthesize progressView = _progressView;
  7. - (void)viewDidLoad
  8. {
  9. [superviewDidLoad];
  10. }
  11. - (IBAction)startIndicator:(id)sender {
  12. //初始化指示器
  13. self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 200, 30, 30)];
  14. /*
  15. 指定指示器的类型
  16. 一共有三种类型:
  17. UIActivityIndicatorViewStyleWhiteLarge   //大型白色指示器
  18. UIActivityIndicatorViewStyleWhite      //标准尺寸白色指示器
  19. UIActivityIndicatorViewStyleGray    //灰色指示器,用于白色背景
  20. */
  21. self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
  22. //停止后是否隐藏(默认为YES)
  23. self.activityIndicator.hidesWhenStopped = YES;
  24. //将Indicator添加到视图中
  25. [self.viewaddSubview:self.activityIndicator];
  26. //开始转动
  27. [self.activityIndicator startAnimating];
  28. //操作队列
  29. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  30. //设置最大的操作数
  31. [operationQueue setMaxConcurrentOperationCount:1];
  32. //构建一个操作对象,selector指定的方法是在另外一个线程中运行的
  33. NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
  34. selector:@selector(runIndicator) object:nil];
  35. //将操作加入队列,此时后台线程开始执行
  36. [operationQueue addOperation:operation];
  37. }
  38. - (IBAction)startProgress:(id)sender {
  39. self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(70, 260, 180, 20)];
  40. /*
  41. 设置风格属性
  42. 有两种风格属性:
  43. UIProgressViewStyleDefault
  44. UIProgressViewStyleBar
  45. */
  46. self.progressView.progressViewStyle = UIProgressViewStyleDefault;
  47. //设置进度,值为0——1.0的浮点数
  48. //    self.progressView.progress = .5;
  49. [self.viewaddSubview:self.progressView];
  50. //设定计时器,每隔1s调用一次runProgress方法
  51. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(runProgress)   userInfo:nil repeats:YES];
  52. }
  53. //在状态栏显示有网络请求的提示器
  54. - (IBAction)startNetWork:(id)sender {
  55. UIApplication *app = [UIApplication sharedApplication];
  56. if (app.isNetworkActivityIndicatorVisible) {
  57. app.networkActivityIndicatorVisible = NO;
  58. }else {
  59. app.networkActivityIndicatorVisible = YES;
  60. }
  61. }
  62. -(void)runIndicator
  63. {
  64. //开启线程并睡眠三秒钟
  65. [NSThread sleepForTimeInterval:3];
  66. //停止UIActivityIndicatorView
  67. [self.activityIndicator stopAnimating];
  68. }
  69. //增加progressView的进度
  70. -(void)runProgress
  71. {
  72. self.progressView.progress += .1;
  73. }
  74. - (void)viewDidUnload
  75. {
  76. [superviewDidUnload];
  77. // Release any retained subviews of the main view.
  78. [self.activityIndicator release];
  79. [self.progressView release];
  80. }
  81. @end

编译运行后效果如下:

ios之UIActivityIndicatorView的更多相关文章

  1. iOS开发-UIActivityIndicatorView简单使用

    软件开发的时候经常会遇到半天才加载出来数据的情况,不管是程序写的烂,还是说本来网速比较慢,一般都都会给个提示让用户感觉到我们在努力的加载数据,iOS可以通过UIActivityIndicatorVie ...

  2. iOS - UI - UIActivityIndicatorView

    1.UIActivityIndicatorView HUD 指示器 UIActivityIndicatorView * indicatorView = [[UIActivityIndicatorVie ...

  3. [New learn] UIKit 框架类

    NSObject NSObject is the root class of most Objective-C class hierarchies. NSDataAsset The NSDataAss ...

  4. iOS UIActivityIndicatorView 的使用

    UIActivityIndicatorView 非常简单 ,就是一个转圈圈的控件:http://blog.csdn.net/zhaopenghhhhhh/article/details/1209265 ...

  5. iOS UIActivityIndicatorView

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle ...

  6. IOS开发UI基础UIActivityIndicatorView的属性

    UIActivityIndicatorView 1.activityIndicatorViewStyle设置指示器的样式UIActivityIndicatorViewStyleWhiteLarge U ...

  7. iOS开发——UI篇Swift篇&UIActivityIndicatorView

    UIActivityIndicatorView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleStr ...

  8. IOS UIActivityIndicatorView 等待指示器

    自己做的一个 等待指示器 #import <UIKit/UIKit.h> @interface RockIndicatorView : UIView { } @property(nonat ...

  9. 转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发

    活动指示器(UIActivityIndicatorView)可以告知用户有一个操作正在进行中.进度指示器(UIProgressView )也具有同样功能,而且还可以告知用户离操作结束还多远. 这两个指 ...

随机推荐

  1. Label-Free Proteomic Analysis of Exosomes Secreted from THP-1- Derived Macrophages Treated with IFN‑α Identifies Antiviral Proteins Enriched in Exosomes (文献分享一组-张霞)

    文献名:Label-Free Proteomic Analysis of Exosomes Secreted from THP-1- Derived Macrophages Treated with ...

  2. iOS 7:漫谈#define 宏定义(转)

    iOS :漫谈#define 宏定义 #define宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步 ...

  3. UWP Popup 弹出提示框

    一:需求 做一个类似于安卓的弹出消息框,如图.当用户点击下载或者选择时,能够从底部弹出一个提示框,用于提示用户. 二:Popup 类 不需要我们自己额外去写一个弹窗类,微软自己有一个Popup 弹窗类 ...

  4. POJ-1258-Agri Ned

    链接:https://vjudge.net/problem/POJ-1258#author=fuxianda 题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么 ...

  5. dubbo-spring

    一.需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址:创建两个服务模块进行测试 测试预期结果:订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能. 二.工程 ...

  6. 一个页面有相同ID元素的情况分析

    经常会遇到一个页面中有相同定义相同id的情况,从道理上来说,id应该是这个页面中某个元素的唯一标识,所以不应该出现有相同id的情况,否则会产生意想不到的结果.而且各个浏览器的表现也是不一样的.我只做了 ...

  7. asp.net中,跳转页面的几种方式

    js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/java ...

  8. spring mvc添加静态资源访问时@Controller无效的解决

    web.xml中的url-pattern设置为/,添加mvc:resources访问静态资源时,@Controller无效的问题 web.xml: <servlet> <servle ...

  9. Vue.js - Day5 - Webpack

    在网页中会引用哪些常见的静态资源? JS .js .jsx .coffee .ts(TypeScript 类 C# 语言) CSS .css .less .sass .scss Images .jpg ...

  10. 从零开始利用vue-cli搭建简单音乐网站(七)

    这几天完成了歌曲收藏功能,先看最后效果: 新注册用户:“newuser”,进入“我的音乐界面如下所示” 点击新建歌单,输入:“新歌单”,确认,如下: 目前还没有歌曲,打开音乐界面,点击收藏功能,如下, ...