自定义的IndicatorView

个人觉得最好用的方式是:

----下面这个是不带文字的-----

@interface SEIndicatorView : UIView

- (void)show;

- (void)hide;

@end



#import "SEIndicatorView.h"

@interface SEIndicatorView()

@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;

@end

@implementation SEIndicatorView

- (id)init
{
    self = [super init];
    
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5];
        
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _indicatorView.center = self.center;
        
        [self addSubview:_indicatorView];
        
        [_indicatorView startAnimating];
    }
    
    return self;
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
}

- (void)hide
{
    [_indicatorView stopAnimating];
    [self removeFromSuperview];
}

@end



使用的地方:

SEIndicatorView *indicatorView = [[SEIndicatorView alloc] init];
    [indicatorView show];



----下面是带有文字的----

@interface LoadingView : UIView

@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;

@end



- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
      _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];      
      
      // Custom a view with an indicator and a label
      UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, centerViewSize.width, centerViewSize.height)];
      centerView.backgroundColor = [UIColor blackColor];
      centerView.layer.cornerRadius = 8.0;
      
      _indicatorView.frame = CGRectMake(centerView.center.x - _indicatorView.frame.size.width/2, 15.0, _indicatorView.frame.size.width, _indicatorView.frame.size.height);
      
      [centerView addSubview:_indicatorView];
      
      UILabel *lblLoading = [[UILabel alloc] initWithFrame:lblLoadingFrame];
      lblLoading.text = NSLocalizedString(@"Indicator.Message.Loading", nil);
      lblLoading.textColor = [UIColor whiteColor];
      lblLoading.font = [UIFont systemFontOfSize:15.0];
      lblLoading.backgroundColor = [UIColor clearColor];
      lblLoading.textAlignment = NSTextAlignmentCenter;
      
      [centerView addSubview:lblLoading];
      
      centerView.center = self.center;
      [self addSubview:centerView];
      
      [_indicatorView startAnimating];
      
      [self setBackgroundColor:[UIColor blackColor]];
      [self setAlpha:0.5];
    }
  
    return self;
}

@end



使用的地方:

_loadingView = [[LoadingView alloc] initWithFrame:self.view.frame];



- (void)startIndicatorAnimation {
  [self.view endEditing:YES];
  [self.view addSubview:_loadingView];
}

- (void)stopIndicatorAnimation {
  sleep(1);
  [_loadingView removeFromSuperview];
}



部分代码可能比较陈旧,之前写的现在整理了出来。也没有细看有什么缺陷,欢迎行内人士热情拍砖。

转载于:https://my.oschina.net/megan/blog/181133

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值