Object-c基础 加载中 控件

来源:互联网 发布:合肥博然软件怎么样 编辑:程序博客网 时间:2024/05/21 22:29

使用iPhone经常会看见一朵菊花旋转,那这个如何使用呢?我们来看看。

其实非常简单,只需要简单的控件UIActivityIndicatorView就可以了。


初始化方式

- (instancetype)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style

- (instancetype)initWithFrame:(CGRect)frame

其中的第一个初始化方法参数style是个枚举类型。

typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {

    UIActivityIndicatorViewStyleWhiteLarge,

    UIActivityIndicatorViewStyleWhite,

    UIActivityIndicatorViewStyleGray,

};

它是用来设置菊花样式的。对应的图片如下:

UIActivityIndicatorViewStyleWhiteLarge  大小是(37,37)     

UIActivityIndicatorViewStyleWhite  大小是(22,22)      

UIActivityIndicatorViewStyleGray   大小是(22,22)     

背景颜色不属于它们的样式,只是我界面的颜色。


对外方法

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. - (void)startAnimating;  //开启动画,也就是开始旋转。  
  2. - (void)stopAnimating;   //停止动画,旋转。  
  3. - (BOOL)isAnimating;     //获取状态 ,0 NO 表示正在旋转,1 YES 表示没有旋转。  


属性

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. @property(nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle; // 默认是UIActivityIndicatorViewStyleWhite  
  2. @property(nonatomicBOOL                         hidesWhenStopped;  // default is YES. 设置动画结束是否隐藏控件。  
  3.   
  4. @property (nullable, readwritenonatomicstrongUIColor *color  

例子代码:
[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #import "ViewController.h"  
  2.   
  3.   
  4. @interface ViewController (){  
  5.     UIActivityIndicatorView *_indicator;  
  6. }  
  7. @end  
  8.   
  9. @implementation ViewController  
  10.   
  11. - (void)viewDidLoad {  
  12.     [super viewDidLoad];  
  13.     _indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];  
  14.     //设置显示位置  
  15.     _indicator.center = CGPointMake(100.0f125.0f);  
  16.     //将这个控件加到父容器中。  
  17.     [self.view addSubview:_indicator];  
  18. }  
  19. - (IBAction)startAnimation:(id)sender {  
  20.     [_indicator startAnimating];  
  21. }  
  22. - (IBAction)stopAnimation:(id)sender {  
  23.     [_indicator stopAnimating];  
  24. }  

点击开启控件显示并旋转。

点击关闭,控件停止旋转并隐藏。

当设置控件属性 

_indicator.hidesWhenStopped = NO;

刚进入这个界面会显示控件。并且停止旋转也会显示,只是没有在转动而已。


设置背景颜色

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //设置背景颜色  
  2. _indicator.backgroundColor = [UIColor blueColor]  
 blueColor     redColor


设置控件颜色 即旋转部分颜色

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //设置菊花颜色。ios5 开始有这个属性  
  2. _indicator.color = [UIColor greenColor];  
greenColor     blueColor
 

另一个初始化方法

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. _indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(1001255050)];  
  2.     //设置背景颜色  
  3. _indicator.backgroundColor = [UIColor blueColor];  
控件中菊花等大小是不变的。这个只是设置这个控件的大小。如图:

0 0
原创粉丝点击