OC使用UIActivityIndicatorView实现简单的菊花图

本文介绍了如何在Objective-C中使用UIActivityIndicatorView创建一个简单的加载指示器,即菊花图。详细讲解了创建LoadingView视图类以及展示和隐藏菊花图的方法。
摘要由CSDN通过智能技术生成

1.创建视图类LoadingView

@interface LoadingView : UIView
+ (void)show;
+ (void)dismiss;
@end
#import "LoadingView.h"
@interface LoadingView ()
@property (nonatomic, strong)UIActivityIndicatorView *aiv;
@end
@implementation LoadingView
+ (LoadingView *)sharedView {
    static LoadingView *lv = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lv = [[LoadingView alloc]init];
    });
    return  lv;
}
/**初始化*/
- (instancetype)init {
    if ([super init]) {
        self.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - HOME_INDICATOR_HEIGHT);
        self.backgroundColor = [UIColor clearColor];
        [[self lastWidow] addSubview:self];
        [self addSubview:self.aiv];
    }
    return self;
}
- (UIActivityIndicatorView *)aiv {
    if (!_aiv) {
        /**设置菊花样式*/
        _aiv = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _aiv.color = [UIColor blackColor];
//        _aiv.backgroundColor = [UIColor grayColor];
        _aiv.center = self.center;
        _aiv.hidesWhenStopped = YES;
        //使用仿射变换放大菊花图
        CGAffineTransform transform = CGAffineTransformMakeScale(1.2, 1.2);
        _aiv.transform = transform;
    }
    return _aiv;
}
/**展示菊花图*/
+ (void)show {
    [[self sharedView] showLoadingView];
}
- (void)showLoadingView {
    self.hidden = NO;
    [self.aiv startAnimating];
}
/**隐藏菊花图*/
+ (void)dismiss {
    [[self sharedView] dismissLoadingView];
}
- (void)dismissLoadingView {
    self.hidden = YES;
    [self.aiv stopAnimating];
}
#pragma mark - 获取最上层window
- (UIWindow *)lastWidow{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for (UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) {
            return window;
        }
    }
    return [UIApplication sharedApplication].keyWindow;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

3.使用方法:
展示:

[LoadingView show];

隐藏:

[LoadingView dismiss];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值