https://blog.csdn.net/weixin_33717117/article/details/92627996
使用系统UIActivityIndicatorView控件:
//
// ViewController.m
// UIActivityIndicatorView指示器控件
//
// Created by Liu,Wenbo(TBRD) on 2020/5/28.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
#import "ViewController.h"
#import "MyUIActivityIndicatorView.h"
@interface ViewController ()
{
MyUIActivityIndicatorView *_view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#pragma mark - 使用系统指示器控件
//初始化一个指示器控件
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 100, 100, 100)];
//设置指示器控件的颜色
indicatorView.color = [UIColor redColor];
//设置指示器样式
indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
/**
typedef enum {
UIActivityIndicatorViewStyleWhiteLarge, //白色圆圈 但是要大些
UIActivityIndicatorViewStyleWhite, //白色圆圈
UIActivityIndicatorViewStyleGray, //灰色圆圈
} UIActivityIndicatorViewStyle;
*/
//开启动画,必须调用,否则无法显示
[indicatorView startAnimating];
[self.view addSubview:indicatorView];
@end
自定义UIActivityIndicatorView控件:
MyUIActivityIndicatorView.h
//
// MyUIActivityIndicatorView.h
// UIActivityIndicatorView指示器控件
//
// Created by Liu,Wenbo(TBRD) on 2020/5/28.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
//自定义指示器类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyUIActivityIndicatorView : UIView
-(void)show;
-(void)unshow;
@property(nonatomic,assign)BOOL isShow;
@end
NS_ASSUME_NONNULL_END
MyUIActivityIndicatorView.m
//
// MyUIActivityIndicatorView.m
// UIActivityIndicatorView指示器控件
//
// Created by Liu,Wenbo(TBRD) on 2020/5/28.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
#import "MyUIActivityIndicatorView.h"
@implementation MyUIActivityIndicatorView
{
UIImageView *_indicatorView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
_indicatorView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 20, 200, 40, 40)];
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for(int i = 0;i < 8;i++){
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%d.png",i]];
[imageArray addObject:img];
}
_indicatorView.animationImages = imageArray;
_indicatorView.animationDuration = 0.5;//设置周期
_indicatorView.animationRepeatCount = 0;//设置重复次数 0表示无限播放
[self addSubview:_indicatorView];
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];//alpha表示透明度
}
return self;
}
-(void)show{
UIWindow *window = [UIApplication sharedApplication].keyWindow;//主window对象
self.isShow = YES;
[window addSubview:self];
}
-(void)unshow{
self.isShow = NO;
[self removeFromSuperview];
}
@end
使用自定义控件:
//
// ViewController.m
// UIActivityIndicatorView指示器控件
//
// Created by Liu,Wenbo(TBRD) on 2020/5/28.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
#import "ViewController.h"
#import "MyUIActivityIndicatorView.h"
@interface ViewController ()
{
MyUIActivityIndicatorView *_view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark - 使用自定义指示控件
_view = [[MyUIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 60)];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
if(_view.isShow){
[_view unshow];
}
else{
[_view show];
}
}
@end