iOS技术分享程序员

iOS开发-自定制网络加载提示框RHProgressHUD

2017-12-21  本文已影响90人  it_hao528
winter.jpg

前言:对于网络加载的提示框,相信大家对MBProgressHUD和SVProgressHUD都很熟悉,并且用起来也很顺手。不过有时候使用过程中可能会遇到一些问题不好解决或者跟自己的需求有一些差别,这就需要我们来进行相应的修改或者进行自定制。在此给大家介绍一个简单的网络加载提示框,供大家参考改善。

话不多说,直接上代码。
首先创建一个继承自UIView的类,我这里叫RHProgressView,在.h里边外漏一个构造方法如下:

@interface RHProgressView : UIView

- (instancetype)initWithFrame:(CGRect)frame tip:(NSString *)tip;
@end

接下来是在.m中来实现:

#import "RHProgressView.h"

@interface RHProgressView ()

@property (nonatomic, strong) UIView * bgView;
@property (nonatomic, strong) UIActivityIndicatorView * activity;
@end
@implementation RHProgressView

- (instancetype)initWithFrame:(CGRect)frame tip:(NSString *)tip {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        [self addSubviewsWithTip:tip];
    }
    return self;
}

- (void)addSubviewsWithTip:(NSString *)tip {
    
    [self addSubview:self.bgView];
    _bgView.frame = CGRectMake(0, 0, SS(80), SS(80));
    [_bgView addSubview:self.activity];
    _activity.frame = CGRectMake(0, SS(20), SS(40), SS(40));
    _activity.center = CGPointMake(_bgView.center.x, _activity.center.y);
    
    if (tip && tip.length > 0) {
        
        UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, SS(60), SS(80), SS(41))];
        textView.textContainerInset = UIEdgeInsetsMake(SS(10), SS(10), SS(15), SS(10));
        textView.font = [UIFont systemFontOfSize:SS(15)];
        textView.textColor = [UIColor whiteColor];
        textView.textAlignment = NSTextAlignmentCenter;
        textView.editable = NO;
        textView.text = tip;
        textView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        CGFloat bgWidth = SS(80);
        CGFloat width = [self getTextViewWidthByText:tip font:textView.font];
        if (width > SS(80)) {
            
            if (width > SS(180)) {
                
                CGFloat height = [self getTextViewHeightByText:tip font:textView.font width:SS(180)];
                textView.frame = CGRectMake(0, SS(60), SS(180), height);
                bgWidth = SS(180);
            } else {
                
                bgWidth = width;
                textView.frame = CGRectMake(0, SS(60), width, SS(41));
            }
        }
        _bgView.frame = CGRectMake(0, 0, bgWidth, SS(60) + textView.frame.size.height);
        textView.center = CGPointMake(_bgView.center.x, textView.center.y);
        _activity.center = CGPointMake(_bgView.center.x, _activity.center.y);
        [_bgView addSubview:textView];
    }
    CGFloat width = self.frame.size.width > 0 ? self.frame.size.width : [UIScreen mainScreen].bounds.size.width;
    CGFloat height = self.frame.size.height > 0 ? self.frame.size.height : [UIScreen mainScreen].bounds.size.height;
    _bgView.center = CGPointMake(width/2, height/2);
}

#pragma mark - private

- (CGFloat)getTextViewWidthByText:(NSString *)text font:(UIFont *)font {
    
    UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, MAXFLOAT, 0)];
    textView.textContainerInset = UIEdgeInsetsMake(SS(10), SS(10), SS(15), SS(10));
    textView.text = text;
    textView.font = font;
    textView.editable = NO;
    [textView sizeToFit];
    CGFloat width = textView.frame.size.width;
    return width;
}

- (CGFloat)getTextViewHeightByText:(NSString *)text font:(UIFont *)font width:(CGFloat)width {
    
    UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    textView.textContainerInset = UIEdgeInsetsMake(SS(10), SS(10), SS(15), SS(10));
    textView.text = text;
    textView.font = font;
    textView.editable = NO;
    [textView sizeToFit];
    CGFloat height = textView.frame.size.height;
    return height;
}

#pragma mark - setter and getter

- (UIView *)bgView {
    
    if (!_bgView) {
        
        UIView * view = [[UIView alloc] init];
        view.layer.cornerRadius = SS(8);
        view.clipsToBounds = YES;
        view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.85];
        _bgView = view;
    }
    return _bgView;
}

- (UIActivityIndicatorView *)activity {
    
    if (!_activity) {
        
        UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] init];
        activity.color = [UIColor blackColor];
        activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//        activity.transform = CGAffineTransformMakeScale(1.2, 1.2);
        [activity startAnimating];
        _activity = activity;
    }
    return _activity;
}

@end

其中的SS()是我写的一个基于4.7寸屏尺寸等比例缩放的宏,大家修改成需要的就行了。

下面是再创建一个工具类RHProgressHUD来对上边定制的view来进行操作。如下:

#import <Foundation/Foundation.h>

@interface RHProgressHUD : NSObject

/**
 添加hud
 
 @param targetView  要添加的view
 @param tip         显示的文字
 */
+ (void)showHudOnView:(UIView *)targetView tip:(NSString *)tip;

/**
 移除hud
 
 @param targetView  hud所在的view
 */
+ (void)removeFromView:(UIView *)targetView;

@end
#import "RHProgressHUD.h"
#import "RHProgressView.h"

@implementation RHProgressHUD

/**
 添加hud

 @param targetView  要添加的view
 @param tip         显示的文字
 */
+ (void)showHudOnView:(UIView *)targetView tip:(NSString *)tip {
    
    [self removeFromView:targetView];
    RHProgressView * progressView = [[RHProgressView alloc] initWithFrame:CGRectMake(0, 0, targetView.frame.size.width, targetView.frame.size.height) tip:tip];
    [targetView addSubview:progressView];
}

/**
 移除hud

 @param targetView  hud所在的view
 */
+ (void)removeFromView:(UIView *)targetView {
    
    for (UIView * view in targetView.subviews) {
        
        if ([view isKindOfClass:[RHProgressView class]]) {
            
            [view removeFromSuperview];
            break;
        }
    }
}
@end

使用的话就只需要调用RHProgressHUD的类方法就可以了。非常简单的一次封装,大家可以根据自己的需求来进行相应的修改以定制出符合自己的网络加载框。

最后,希望能够帮到有需要的朋友们,愿我们能够一起学习进步,在开发的道路上越走越顺利!

上一篇下一篇

猜你喜欢

热点阅读