Object-c基础 加载中 控件

使用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];  
控件中菊花等大小是不变的。这个只是设置这个控件的大小。如图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值