使用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)
背景颜色不属于它们的样式,只是我界面的颜色。
对外方法
- - (void)startAnimating;
- - (void)stopAnimating;
- - (BOOL)isAnimating;
属性
- @property(nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
- @property(nonatomic) BOOL hidesWhenStopped;
-
- @property (nullable, readwrite, nonatomic, strong) UIColor *color
例子代码:
- #import "ViewController.h"
-
-
- @interface ViewController (){
- UIActivityIndicatorView *_indicator;
- }
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- _indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
-
- _indicator.center = CGPointMake(100.0f, 125.0f);
-
- [self.view addSubview:_indicator];
- }
- - (IBAction)startAnimation:(id)sender {
- [_indicator startAnimating];
- }
- - (IBAction)stopAnimation:(id)sender {
- [_indicator stopAnimating];
- }
点击开启控件显示并旋转。
点击关闭,控件停止旋转并隐藏。
当设置控件属性
_indicator.hidesWhenStopped = NO;
刚进入这个界面会显示控件。并且停止旋转也会显示,只是没有在转动而已。
设置背景颜色
-
- _indicator.backgroundColor = [UIColor blueColor]
blueColor
redColor
设置控件颜色 即旋转部分颜色
-
- _indicator.color = [UIColor greenColor];
greenColor
blueColor
另一个初始化方法
- _indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 125, 50, 50)];
-
- _indicator.backgroundColor = [UIColor blueColor];
控件中菊花等大小是不变的。这个只是设置这个控件的大小。如图: