UIActivityIndicatorView

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值