iOS UIActivityIndicatorView用法(小菊花旋转)
UIActivityIndicatorView初始化方式
- -(instancetype)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
- -(instancetype)initWithFrame:(CGRect)frame
其中的第一个初始化方法参数style是个枚举类型。
typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {
UIActivityIndicatorViewStyleWhiteLarge,
UIActivityIndicatorViewStyleWhite,
UIActivityIndicatorViewStyleGray,
};
代码示例
这是我在Main.storyboard拖拽的两个按钮,是为了控制开始旋转和停止旋转,也可以自己手写代码呦
#import "ViewController.h"
@interface ViewController ()
- (IBAction)startAction:(UIButton *)sender;
- (IBAction)stopAction:(UIButton *)sender;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 100, 60, 60)];
self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
[self.view addSubview:self.activityIndicator];
//属性值是一个枚举变量,只有三个值:
//UIActivityIndicatorViewStyleWhite; 白色圆圈
//UIActivityIndicatorViewStyleWhiteLarge; 白色圆圈 但是要大些
//UIActivityIndicatorViewStyleGray; 灰色圆圈
// self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
self.activityIndicator.frame= CGRectMake(100, 100, 100, 100);
//设置小菊花颜色
self.activityIndicator.color = [UIColor redColor];
//设置背景颜色
self.activityIndicator.backgroundColor = [UIColor cyanColor];
//刚进入这个界面会显示控件。并且停止旋转也会显示,只是没有在转动而已。
self.activityIndicator.hidesWhenStopped = NO;
}
- (IBAction)startAction:(UIButton *)sender {
[self.activityIndicator startAnimating];
}
- (IBAction)stopAction:(UIButton *)sender {
[self.activityIndicator stopAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是效果图
-
控件中菊花大小是不变的。只是设置控件的大小。