winter.jpg
前言:對于網(wǎng)絡(luò)加載的提示框呀袱,相信大家對MBProgressHUD和SVProgressHUD都很熟悉趣钱,并且用起來也很順手唠椭。不過有時(shí)候使用過程中可能會遇到一些問題不好解決或者跟自己的需求有一些差別憎账,這就需要我們來進(jìn)行相應(yīng)的修改或者進(jìn)行自定制面氓。在此給大家介紹一個(gè)簡單的網(wǎng)絡(luò)加載提示框,供大家參考改善河咽。
話不多說钠右,直接上代碼。
首先創(chuàng)建一個(gè)繼承自UIView
的類忘蟹,我這里叫RHProgressView
飒房,在.h
里邊外漏一個(gè)構(gòu)造方法如下:
@interface RHProgressView : UIView
- (instancetype)initWithFrame:(CGRect)frame tip:(NSString *)tip;
@end
接下來是在.m
中來實(shí)現(xiàn):
#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()
是我寫的一個(gè)基于4.7寸屏尺寸等比例縮放的宏,大家修改成需要的就行了媚值。
下面是再創(chuàng)建一個(gè)工具類RHProgressHUD
來對上邊定制的view來進(jìn)行操作狠毯。如下:
#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
使用的話就只需要調(diào)用RHProgressHUD
的類方法就可以了。非常簡單的一次封裝杂腰,大家可以根據(jù)自己的需求來進(jìn)行相應(yīng)的修改以定制出符合自己的網(wǎng)絡(luò)加載框垃你。
最后椅文,希望能夠幫到有需要的朋友們喂很,愿我們能夠一起學(xué)習(xí)進(jìn)步,在開發(fā)的道路上越走越順利皆刺!