#import "ViewController.h"
#import "MyActivityIndicatorView.h";
@interface ViewController ()
{
MyActivityIndicatorView * _view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
// Do any additional setup after loading the view, typically from a nib.
//UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(160,100,0,0)];
UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicatorView];
indicatorView.center=CGPointMake(160,100);
self.view.backgroundColor = [UIColor redColor];
[indicatorView startAnimating];
indicatorView.color = [UIColor grayColor];
*/
_view = [[MyActivityIndicatorView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-60)];
_view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置按钮的frame
button.frame = CGRectMake(self.view.frame.size.width/2-20,self.view.frame.size.height-50,30,40);
//设置背景颜色
button.backgroundColor = [UIColor redColor];
//设置按钮的标题
[button setTitle:@"点击" forState:UIControlStateNormal];
[self.view addSubview:button];
//为按钮添加一个方法
[button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if(_view.isShow){
[_view unshow];
}else{
[_view show];
}
}
-(void)clickBtn:(UIButton *)btn{
if(_view.isShow){
[_view unshow];
}else{
[_view show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface MyActivityIndicatorView : UIView
-(void)show;
-(void)unshow;
@property (nonatomic, assign)BOOL isShow;
@end
#import "MyActivityIndicatorView.h"
@implementation MyActivityIndicatorView {
UIImageView *_indicatorView;
}
- (void)show {
//始终在最上面
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:self];
self.isShow = YES;
}
- (void)unshow {
self.isShow = NO;
[self removeFromSuperview];
}
- (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 *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%d.png", i]];
[imageArray addObject:image];
}
_indicatorView.animationImages = imageArray;
_indicatorView.animationDuration = 1;
_indicatorView.animationRepeatCount = 0;
[_indicatorView startAnimating];
[self addSubview:_indicatorView];
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
}
return self;
}
@end