OC--链式编程学习(简单封装MBProgressHUD用例)
2017-02-26 本文已影响421人
啊哈呵
简单实现
BBPerson.h
#import <Foundation/Foundation.h>
@class BBPerson;
typedef BBPerson *(^BBChainStringBlock)(NSString *);
@interface BBPerson : NSObject
@property (nonatomic, strong) NSString *nameString;/**< */
@property (nonatomic, strong) NSString *ageString;/**< */
- (BBChainStringBlock)name;
- (BBChainStringBlock)age;
@end
BBPerson.m
#import "BBPerson.h"
@implementation BBPerson
- (BBChainStringBlock)name {
BBChainStringBlock block = ^(NSString * aString) {
_nameString = aString;
return self;
};
return block;
}
- (BBChainStringBlock)age {
BBChainStringBlock block = ^(NSString * aString) {
_ageString = aString;
return self;
};
return block;
}
@end
使用例子
BBPerson *person = [BBPerson new];
// 使用方式可以如下:
person.age(@"18");
person.name(@"XXX");
person.name(@"XXX").age(@"18");
person.age(@"18").name(@"XXX");
NSLog(@"nameString=%@ ageString=%@",person.nameString,person.ageString);
// 解析:
// 其实person.name返回一个block,
BBChainStringBlock nameBlock = person.name;
nameBlock(@"XXX");// 这个block就是把 _nameString = @"XXX"; return self;
nameBlock(@"XXX").age(@"18");// nameBlock(@"XXX")是返回self,即使person对象,所以可以链式编程
增加一个make方法
+ (BBPerson *)makeConstraints:(void(^)(BBPerson *))block {
BBPerson *person = [[BBPerson alloc] init];
block(person);
return person;
}
使用make方法
// 就这么new一个person实例了
BBPerson *person = [BBPerson makeConstraints:^(BBPerson *maker) {
maker.name(@"WWW").age(@"18");
}];
// 以后需要括展属性,比如性别sex,写好sex的block
BBPerson *person1 = [BBPerson makeConstraints:^(BBPerson *maker) {
maker.name(@"WWW").age(@"18").sex(@"男");
}];
真实项目开发过程中,为了避免第三方库入侵项目代码内部,经常需要进一步封装第三方库:
1.项目与第三方尽量解耦,
2.使用、修改替换方便。
OC的封装就是各种条件都写好,参数少还可以,参数多封装的方法就多。
比如封装一下MBProgressHUD:动画参数、时间参数、显示的文字、显示在哪个view上、显示自定义的小图片、能不能操作hud背后界面。如果把这些参数都各种全组合方法一个一个写,那要死人哦。
// 一般的show,有各种组合、有maskType--能操作、不能操作
+ (void)show;
+ (void)showWithMaskType:(PSProgressHUDMaskType)maskType;
+ (void)showWithStatus:(NSString*)status;
+ (void)showWithStatus:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showProgress:(float)progress;
+ (void)showProgress:(float)progress maskType:(PSProgressHUDMaskType)maskType;
+ (void)showProgress:(float)progress status:(NSString*)status;
+ (void)showProgress:(float)progress status:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showInfoWithStatus:(NSString*)status;
+ (void)showInfoWithStatus:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showSuccessWithStatus:(NSString*)status;
+ (void)showSuccessWithStatus:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showErrorWithStatus:(NSString*)status;
+ (void)showErrorWithStatus:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showStatus:(NSString*)status;
+ (void)showStatus:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
+ (void)showImage:(UIImage*)image status:(NSString*)status;
+ (void)showImage:(UIImage*)image status:(NSString*)status maskType:(PSProgressHUDMaskType)maskType;
如果使用链式编程,就可以解决参数过多的麻烦了。
然后自己试着用链式编程方式,封装一下MBProgressHUD
直接上代码
MBProgressHUD.h
#import <Foundation/Foundation.h>
#import "MBProgressHUD.h"
#define kPSProgressHUD [PSProgressHUD new]
typedef NS_ENUM(NSUInteger, PSHUDMaskType) {
PSHUDMaskType_None = 0, //允许操作其他UI
PSHUDMaskType_Clear, //不允许操作
};
typedef NS_ENUM(NSUInteger, PSHUDInViewType) {
PSHUDInViewType_KeyWindow = 0, //UIApplication.KeyWindow
PSHUDInViewType_CurrentView, //CurrentViewController.view
};
typedef NS_ENUM(NSUInteger, PSProgressType) {
PSProgressType_HorizontalBar = 0, //水平进度条
PSProgressType_AnnularBar, //环形进度条
};
//PSProgressHUD链式编程语法的使用🌰
/*
一、Loading
显示:[PSProgressHUD showLoadingCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_CurrentView).message(@"你好吗?");
}];
消失:[PSProgressHUD cancelLoadingCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_CurrentView);
}];
二、showHandleMessage
显示:[PSProgressHUD showHandleMessageCalculators:^(PSProgressHUD *make) {
make.afterDelay(10).message(@"你好啊");
}];
三、showUploadProgress
使用:MBProgressHUD *hud=[PSProgressHUD showUploadProgressCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_CurrentView).message(@"我是进度条");
}];
更新进度条:hud.progress=0.5;
消失:
(1)[hud hideAnimated:YES]; (直接使用就可以,前面已经生成MBProgressHUD *hud)
(2)[PSProgressHUD cancelUploadProgressCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_CurrentView);
}];
*/
@interface PSProgressHUD : NSObject
/**
显示Loading HUD
@param block 配置各种参数
@return MBProgressHUD对象
*/
+ (MBProgressHUD *)showLoadingCalculators:(void (^)(PSProgressHUD * make))block;
/**
消失Loading HUD
@param block 必须配置与showLoading对应的InView
*/
+ (void)cancelLoadingCalculators:(void (^)(PSProgressHUD * make))block;
/**
显示HandleMessage HUD
@param block 配置各种参数
@return MBProgressHUD对象
*/
+ (MBProgressHUD *)showHandleMessageCalculators:(void (^)(PSProgressHUD * make))block;
/**
显示Loading HUD
@param block 配置各种参数
@return MBProgressHUD对象
*/
+ (MBProgressHUD *)showUploadProgressCalculators:(void (^)(PSProgressHUD * make))block;
/**
消失UploadProgress HUD
@param block 必须配置与showUploadProgress对应的InView
*/
+ (void)cancelUploadProgressCalculators:(void (^)(PSProgressHUD * make))block;
#pragma mark - 下面的代码,就是设置保存相应的参数,返回self,
// self=self.message(@"文字"),分两步
// (1)第一步:self.message首先是返回一个block;
// (2)第二步:self=self.messageblock(@"文字") block里面是{ self.msg=@"文字"; 返回self }.
// 对应一般的语法就是:self=[self message:@"文字"];就是这么个意思
/**
.showMessage(@"需要显示的文字")
*/
- (PSProgressHUD *(^)(NSString *))message;
/**
.animated(YES)是否动画,YES动画,NO不动画
*/
- (PSProgressHUD *(^)(BOOL))animated;
/**
.inView(view)
有特殊需要inView的才使用,一般使用.inViewType()
*/
- (PSProgressHUD *(^)(UIView *))inView;
/**
.inViewType(inViewType) 指定的InView
PSHUDInViewType_KeyWindow--KeyWindow,配合MaskType_Clear,就是全部挡住屏幕不能操作了,只能等消失
PSHUDInViewType_CurrentView--当前的ViewController,配合MaskType_Clear,就是view不能操作,但是导航栏能操作(例如返回按钮)。
*/
- (PSProgressHUD *(^)(PSHUDInViewType))inViewType;
/**
.maskType(MaskType) HUD显示是否允许操作背后,
PSHUDMaskType_None:允许
PSHUDMaskType_Clear:不允许
*/
- (PSProgressHUD *(^)(PSHUDMaskType))maskType;
/**
.customView(view),设置customView
注:只对.showMessage(@"")有效
*/
- (PSProgressHUD *(^)(UIView *))customView;
/**
.customView(iconName),带有小图标、信息,
iconName:小图标名字
注:只对.showMessage(@"")有效
*/
- (PSProgressHUD *(^)(NSString *))customIconName;
/**
.afterDelay(2)消失时间,默认是2秒
注:只对.showHandleMessageCalculators有效
*/
- (PSProgressHUD *(^)(NSTimeInterval))afterDelay;
/**
progressType()设置进度条类型
PSProgressType_HorizontalBar--水平进度条
PSProgressType_AnnularBar-----环形进度条
注:只对.showUploadProgressCalculators有效
*/
- (PSProgressHUD *(^)(PSProgressType))progressType;
MBProgressHUD.m
#import "PSProgressHUD.h"
#import "UIApplication+AppRootViewController.h"
@interface PSProgressHUD ()
{
}
//全都可以使用的参数
@property(nonatomic, strong) UIView *ps_inView;/**<hud加在那个view上*/
@property(nonatomic, assign) BOOL ps_animated;/**<是否动画显示、消失*/
@property(nonatomic, assign) PSHUDMaskType ps_maskType;/**<hud背后的view是否还可以操作*/
//只有showHandleMessage可以使用的属性
@property(nonatomic, strong) UIView *ps_customView;/**<自定义的view*/
@property(nonatomic, strong) NSString *ps_customIconName;/**<自定义的小图标*/
@property(nonatomic, strong) NSString *ps_message;/**<hud上面的文字*/
@property(nonatomic, assign) NSTimeInterval ps_afterDelay;/**<自动消失时间*/
//只有进度条使用的
@property(nonatomic, assign) PSProgressType ps_progressType;/**<进度条类型:水平横条或者圆形*/
@end
@implementation PSProgressHUD
//简单的显示方法
+ (MBProgressHUD *)showLoading:(NSString *)loadingString{
return [PSProgressHUD showLoadingCalculators:^(PSProgressHUD *make) {
make.message(loadingString);
}];
}
+ (MBProgressHUD *)showLoadingInKeyWindow:(NSString *)loadingString{
return [PSProgressHUD showLoadingCalculators:^(PSProgressHUD *make) {
make.message(loadingString).inViewType(PSHUDInViewType_KeyWindow);
}];
}
+ (MBProgressHUD *)showLoadingCurrentView:(NSString *)loadingString{
return [PSProgressHUD showLoadingCalculators:^(PSProgressHUD *make) {
make.message(loadingString).inViewType(PSHUDInViewType_CurrentView);
}];
}
+ (MBProgressHUD *)showLoadingCalculators:(void (^)(PSProgressHUD * make))block {
//这就是block作为方法参数的用法,平时比较少写,感觉有点绕,脑袋转不过来。
// void (^block)(PSProgressHUD * make)=^(PSProgressHUD *make) {
// //这里可以链式语法,设置各种参数
// make.inViewType(PSHUDInViewType_CurrentView).message(@"你好吗?");
// };
PSProgressHUD *makeObj = [[PSProgressHUD alloc] init];
block(makeObj);
__block MBProgressHUD *hud = nil;
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:makeObj.ps_inView animated:makeObj.ps_animated];
hud.label.text=makeObj.ps_message;
hud.mode = MBProgressHUDModeIndeterminate;
hud.minSize=CGSizeMake(120, 100);
hud.userInteractionEnabled=makeObj.ps_animated;
});
return hud;
}
+ (void)cancelLoading{
[PSProgressHUD cancelLoadingCalculators:^(PSProgressHUD *make) {}];
}
+ (void)cancelLoadingInKeyWindow{
[PSProgressHUD cancelLoadingCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_KeyWindow);
}];
}
+ (void)cancelLoadingCurrentView{
[PSProgressHUD cancelLoadingCalculators:^(PSProgressHUD *make) {
make.inViewType(PSHUDInViewType_CurrentView);
}];
}
+ (void)cancelLoadingCalculators:(void (^)(PSProgressHUD * make))block {
PSProgressHUD *makeObj = [[PSProgressHUD alloc] init];
block(makeObj);
[MBProgressHUD hideHUDForView:makeObj.ps_inView animated:makeObj.ps_animated];
}
+ (MBProgressHUD *)showHandleMessage:(NSString *)handleMsg{
return [PSProgressHUD showHandleMessageCalculators:^(PSProgressHUD *make) {
make.message(handleMsg);
}];
}
+ (MBProgressHUD *)showHandleMessageInKeyWindow:(NSString *)handleMsg{
return [PSProgressHUD showHandleMessageCalculators:^(PSProgressHUD *make) {
make.message(handleMsg).inViewType(PSHUDInViewType_KeyWindow);;
}];
}
+ (MBProgressHUD *)showHandleMessageCurrentView:(NSString *)handleMsg{
return [PSProgressHUD showHandleMessageCalculators:^(PSProgressHUD *make) {
make.message(handleMsg).inViewType(PSHUDInViewType_CurrentView);;
}];
}
+ (MBProgressHUD *)showHandleMessageCalculators:(void (^)(PSProgressHUD * make))block {
PSProgressHUD *makeObj = [[PSProgressHUD alloc] init];
block(makeObj);
__block MBProgressHUD *hud = nil;
dispatch_async(dispatch_get_main_queue(), ^{
hud = [MBProgressHUD showHUDAddedTo:makeObj.ps_inView animated:makeObj.ps_animated];
hud.label.text=makeObj.ps_message;
hud.mode = MBProgressHUDModeText;
hud.userInteractionEnabled=makeObj.ps_maskType;
if (makeObj.ps_customView) {
hud.customView = makeObj.ps_customView;
hud.mode = MBProgressHUDModeCustomView;
}else if (makeObj.ps_customIconName) {
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:makeObj.ps_customIconName]];
hud.mode = MBProgressHUDModeCustomView;
}
[hud hideAnimated:makeObj.ps_animated afterDelay:makeObj.ps_afterDelay];
});
return hud;
}
+ (MBProgressHUD *)showUploadProgress:(NSString *)msg{
return [PSProgressHUD showUploadProgressCalculators:^(PSProgressHUD *make) {
make.message(msg);
}];
}
+ (MBProgressHUD *)showUploadProgressCalculators:(void (^)(PSProgressHUD * make))block{
PSProgressHUD *makeObj = [[PSProgressHUD alloc] init];
block(makeObj);
__block MBProgressHUD *hud = nil;
dispatch_async(dispatch_get_main_queue(), ^{
hud = [MBProgressHUD showHUDAddedTo:makeObj.ps_inView animated:makeObj.ps_animated];
hud.label.text=makeObj.ps_message;
hud.tag=88888;
hud.mode = MBProgressHUDModeText;
hud.userInteractionEnabled=makeObj.ps_maskType;
if (makeObj.ps_progressType==PSProgressType_HorizontalBar) {
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
}else if (makeObj.ps_progressType==PSProgressType_AnnularBar) {
hud.mode = MBProgressHUDModeAnnularDeterminate;
}
});
return hud;
}
+ (void)cancelUploadProgressCalculators:(void (^)(PSProgressHUD * make))block {
PSProgressHUD *makeObj = [[PSProgressHUD alloc] init];
block(makeObj);
MBProgressHUD *hud = (MBProgressHUD *)[makeObj.ps_inView viewWithTag:88888];
[hud hideAnimated:makeObj.ps_animated];
}
- (instancetype)init{
self=[super init];
if (self) {//这里可以设置一些默认的属性
_ps_inView=[UIApplication sharedApplication].keyWindow;
_ps_maskType=PSHUDMaskType_None;
_ps_afterDelay=2;
}
return self;
}
- (PSProgressHUD *(^)(UIView *))inView{
return ^PSProgressHUD *(id obj) {
_ps_inView=obj;
return self;
};
}
- (PSProgressHUD *(^)(UIView *))customView{
return ^PSProgressHUD *(id obj) {
_ps_customView=obj;
return self;
};
}
- (PSProgressHUD *(^)(NSString *))customIconName{
return ^PSProgressHUD *(id obj) {
_ps_customIconName=obj;
return self;
};
}
- (PSProgressHUD *(^)(PSHUDInViewType))inViewType{
return ^PSProgressHUD *(PSHUDInViewType inViewType) {
if (inViewType==PSHUDInViewType_KeyWindow) {
_ps_inView=[UIApplication sharedApplication].keyWindow;
}else if(inViewType==PSHUDInViewType_CurrentView){
_ps_inView=[UIApplication currentViewController].view;
}
return self;
};
}
- (PSProgressHUD *(^)(BOOL))animated{
return ^PSProgressHUD *(BOOL animated) {
_ps_animated=animated;
return self;
};
}
- (PSProgressHUD *(^)(PSHUDMaskType))maskType{
return ^PSProgressHUD *(PSHUDMaskType maskType) {
_ps_maskType=maskType;
return self;
};
}
- (PSProgressHUD *(^)(NSTimeInterval))afterDelay{
return ^PSProgressHUD *(NSTimeInterval afterDelay) {
_ps_afterDelay=afterDelay;
return self;
};
}
- (PSProgressHUD *(^)(NSString *))message{
return ^PSProgressHUD *(NSString *msg) {
_ps_message=msg;
return self;
};
}
- (PSProgressHUD *(^)(PSProgressType))progressType{
return ^PSProgressHUD *(PSProgressType progressType) {
_ps_progressType=progressType;
return self;
};
}
@end
现在只是学习一下链式编程的,如果完善封装MBProgressHUD还需要很多各方面的考虑