在开发过程中 ,越来越多的界面都需要进行友好的容错处理,例如:1、在弱网情况下的接口请求,很容易造成请求失败;2、无网情况下的界面显示;3、推进界面后数据为加载之前,显示的UI等等
直接上代码:
#import <UIKit/UIKit.h>
typedef void(^BtnActionBlock)();
@interface JHLivePlayRemindView : UIView
+ shareRemindView;
- (void)showInView:(UIView *)superView imageName:(NSString *)imageName remindTxt:(NSString *)remindTxt withBtnBlock:(BtnActionBlock)callBack;
- (void)hide;
@end
实现代码:
#import "JHLivePlayRemindView.h"
#import "UIColor+Translate.h"
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface JHLivePlayRemindView ()
{
UIView * containsView;
UIImageView * imageView;
UILabel * remindLbl;
UIButton * refreshBtn ;
}
@property (nonatomic,copy)BtnActionBlock btnBlock;
@end
@implementation JHLivePlayRemindView
+ shareRemindView{
static JHLivePlayRemindView * remindView = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
remindView = [[JHLivePlayRemindView alloc] init];
});
return remindView;
}
- (instancetype)init{
self = [super init];
if (self) {
containsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 265)];
containsView.center = self.center;
[self addSubview:containsView];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 140, 165)];
[containsView addSubview:imageView];
imageView.center = CGPointMake(ScreenWidth/2, imageView.center.y);
remindLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame) + 20, ScreenWidth, 30)];
remindLbl.textColor = [UIColor colorFromHexRGB:@"999999"];
remindLbl.font = [UIFont systemFontOfSize:14.0];
remindLbl.textAlignment = NSTextAlignmentCenter;
[containsView addSubview:remindLbl];
refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(remindLbl.frame)+10, 90, 30)];
[refreshBtn setImage:[UIImage imageNamed:@"JHLivePlayBundle.bundle/refresh.tiff"] forState:UIControlStateNormal];
[refreshBtn setImage:[UIImage imageNamed:@"JHLivePlayBundle.bundle/refreshHighlighted.tiff"] forState:UIControlStateHighlighted];
[containsView addSubview:refreshBtn];
[refreshBtn addTarget:self action:@selector(refreshClick) forControlEvents:UIControlEventTouchUpInside];
refreshBtn.center = CGPointMake(ScreenWidth/2, refreshBtn.center.y);
containsView.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
}
return self;
}
- (void)showInView:(UIView *)superView imageName:(NSString *)imageName remindTxt:(NSString *)remindTxt withBtnBlock:(BtnActionBlock)callBack{
self.backgroundColor = [UIColor whiteColor];
self.frame = superView.bounds;
if ([superView.subviews containsObject:self]) {
return;
}
containsView.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(refreshBtn.frame));
[superView addSubview:self];
[superView bringSubviewToFront:self];
containsView.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2);
imageView.image = [UIImage imageNamed:imageName];
imageView.center = CGPointMake(ScreenWidth/2, imageView.center.y);
remindLbl.text = remindTxt;
refreshBtn.center = CGPointMake(ScreenWidth/2, refreshBtn.center.y);
if (callBack == nil) {
refreshBtn.hidden = YES;
}else{
self.btnBlock = [callBack copy];
refreshBtn.hidden = NO;
}
}
- (void)hide{
self.btnBlock = nil;
[self removeFromSuperview];
}
- (void)refreshClick{
self.btnBlock();
}
@end
开出的方法很简单 ,只有三个方法;
真正起作用的方法是
- (void)showInView:(UIView *)superView imageName:(NSString *)imageName remindTxt:(NSString *)remindTxt withBtnBlock:(BtnActionBlock)callBack;
superView定义为需要添加提示的视图,imageName为本地的图片名称,remindTxt 为提示的文字信息,callback 为给提示按钮增加的点击事件
在完成此基础方法的扩展上 ,可以做很多事情;
例如:1、增加隐藏按钮的方法,只显示图片和提示文字
2、增加图片是否存在的判断,在无图片的时候增加默认的提示图片等
3、增加文字的修饰颜色,字体大小等
4、增加视图显示的过渡动画
有很多地方可以根据自身的需求进行扩展