UIActivityIndicatorView指示器控件用法总结

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值