iOS开发之-仿支付宝加载框(类似MBProgressHUD)

在项目实际的开发中,MBProgressHUD有时候不能够满足项目需求,所有有时候需要自己去定义。但在开发中也要主要封装中,要高内聚低耦合。以下是,仿支付宝的加载框样式,希望能够帮助大家。能给予好评。喜欢的😍可以加个关注,成为粉丝,互动学习。图片可以到https://github.com/zhangjianios/ZJViewProgressHUD去下载。效果图:直接下来,颜色背景自己去定义把,都是可以自己定义的哦。


//ZJProgressHUD.h

//ZJViewProgressHUD

//http://blog.sina.com.cn/resoftios

//Created by张建on 17/1/3.

//Copyright © 2017年zhangjian. All rights reserved.

//

#import

/**

*@zhangjian01

*@自定义加载加载按钮,更换之前第三方的样式.

*/

typedefvoid(^ZJViewShowHUDCompletionBlock)();

//http://blog.sina.com.cn/resoftios

@interfaceZJProgressHUD :UIView

//加载文字

@property(nonatomic,strong)UILabel*labelTexts;

@property(nonatomic,strong)UIImageView*bgImageView;

@property(assign)BOOLtaskInProgress;

@property(assign)floatminShowTime;

@property(assign)floatgraceTime;

//初始化视图

- (id)initWithView:(UIView*)view;

//开始加载动画

- (void) startRotates:(BOOL)animated;

- (void) startRotate;

//停止加载动画

- (void) stopRotates:(BOOL)animated;

- (void) stopRotate;

@property(copy)ZJViewShowHUDCompletionBlockcompletionBlock;

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(ZJViewShowHUDCompletionBlock)completion;

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue

completionBlock:(ZJViewShowHUDCompletionBlock)completion;

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue;

//http://blog.sina.com.cn/resoftios

@end

//

//ZJProgressHUD.m

//ZJViewProgressHUD

//

//Created by张建on 17/1/3.

//Copyright © 2017年zhangjian. All rights reserved.

//

#import"ZJProgressHUD.h"

#define MDXFrom6(x) ([[UIScreen mainScreen] bounds].size.width/375.0*x)

/**

*zhangjian01

*自定义加载加载按钮,更换之前第三方的样式.

*/

@interfaceZJProgressHUD(){

BOOLanimating;

UIView*bgView;

UIView*bg1;

UIView*maskview;

UIImageView*bgImageView1;

UIImageView*bgImageView2;

}

@property(nonatomic,strong)UIView*contentView;

@property(atomic,strong)NSDate*showStarted;

@property(atomic,strong)NSTimer*minShowTimer;

@property(atomic,strong)NSTimer*graceTimer;

- (void)done;

- (void)showUsingAnimation:(BOOL)animated;

- (void)hideUsingAnimation:(BOOL)animated;

- (void)handleGraceTimers:(NSTimer*)theTimer ;

@end

@implementationZJProgressHUD

- (void)dealloc {

#if !__has_feature(objc_arc)

[labelTexts release];

[graceTimer release];

[minShowTimer release];

[showStarted release];

#if NS_BLOCKS_AVAILABLE

[completionBlock release];

#endif

[superdealloc];

#endif

}

- (id)initWithView:(UIView*)view {

NSAssert(view,@"View must not be nil.");

return[selfinitWithFrame:view.bounds];

}

- (instancetype)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];

if(self) {

self.taskInProgress=NO;

[selflayoutAllSubviews:frame];

}

returnself;

}

- (id)initWithWindow:(UIWindow*)window {

return[selfinitWithView:window];

}

- (void)layoutSubviews {

maskview.frame=CGRectMake(0,0,self.superview.frame.size.width,self.superview.frame.size.height);

//UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];

//CGPoint windowCenter = CGPointMake(keywindow.frame.size.width/2, keywindow.frame.size.height/2);

//CGPoint convertCenter = [keywindow convertPoint:windowCenter toView:self.superview];

//CGPoint convertCenter = [keywindow convertPoint:windowCenter toWindow:self.superview.window];

bgView.center=CGPointMake(self.superview.frame.size.width/2,self.superview.frame.size.height/2);

self.bgImageView.center=CGPointMake(self.superview.frame.size.width/2,self.superview.frame.size.height/2);

//bgView.center = convertCenter;

//self.bgImageView.center = convertCenter;

}

- (void)layoutAllSubviews:(CGRect)frame{

/*创建灰色背景*/

maskview= [[UIViewalloc]initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];

[selfaddSubview:maskview];

maskview.backgroundColor= [UIColorlightGrayColor];

maskview.alpha=0.4;

bgView= [[UIViewalloc]initWithFrame:CGRectMake(MDXFrom6(124), (frame.size.height-64)/2,MDXFrom6(128),MDXFrom6(64))];

bgView.center=self.center;

[maskviewaddSubview:bgView];

self.bgImageView= [[UIImageViewalloc]initWithFrame:bgView.frame];

self.bgImageView.image= [UIImageimageNamed:@"d.png"];

[selfaddSubview:self.bgImageView];

bgImageView1= [[UIImageViewalloc]initWithFrame:CGRectMake(MDXFrom6(17),MDXFrom6(17),MDXFrom6(31),MDXFrom6(31))];

bgImageView1.image= [UIImageimageNamed:@"c1.png"];

[self.bgImageViewaddSubview:bgImageView1];

bgImageView2= [[UIImageViewalloc]initWithFrame:CGRectMake(MDXFrom6(17),MDXFrom6(17),MDXFrom6(31),MDXFrom6(31))];

bgImageView2.image= [UIImageimageNamed:@"c.png"];

[self.bgImageViewaddSubview:bgImageView2];

_labelTexts= [[UILabelalloc]initWithFrame:CGRectMake(bgImageView1.frame.origin.x+bgImageView1.frame.size.width+MDXFrom6(10),0,MDXFrom6(70),MDXFrom6(64))];

[_labelTextssetFont:[UIFontsystemFontOfSize:12]];

_labelTexts.text=@"加载中...";

[self.bgImageViewaddSubview:_labelTexts];

///*创建灰色背景*/

//UIView *maskview = [[UIView alloc] initWithFrame:frame];

//[self addSubview:maskview];

//maskview.backgroundColor = [UIColor blackColor];

//maskview.alpha = 0.5;

//

//bgView = [[UIView alloc]initWithFrame:CGRectMake((frame.size.width - 100- 61 - 10 * 3) / 2, (self.frame.size.height - 64) / 2, 191, 81)];

//bgView.center = self.center;

////bgView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

//[self addSubview:bgView];

//

//UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:bgView.frame];

////bgImageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

////bgImageView.image = [UIImage imageNamed:@"d"];

//bgImageView.backgroundColor = [UIColor whiteColor];

//bgImageView.layer.masksToBounds = YES;

//bgImageView.layer.cornerRadius = 3;

//[self addSubview:bgImageView];

//

//bg1 = [[UIView alloc]initWithFrame:CGRectMake(10, 21, 40, 40)];

////bg1.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

//[bgImageView addSubview:bg1];

//

//bgImageView1 = [[UIImageView alloc] initWithFrame:bg1.frame];

//bgImageView1.image = [UIImage imageNamed:@"c1.png"];

//[bgImageView addSubview:bgImageView1];

//

//bgImageView2 = [[UIImageView alloc] initWithFrame:bg1.frame];

//bgImageView2.image = [UIImage imageNamed:@"c.png"];

//[bgImageView addSubview:bgImageView2];

//

//_labelTexts = [[UILabel alloc]initWithFrame:CGRectMake(bgImageView1.frame.origin.x + 61, 10, 100, 61)];

//[_labelTexts setFont:[UIFont systemFontOfSize:17]];

//_labelTexts.text = @"加载中...";

//[bgImageView addSubview:_labelTexts];

//[self startRotate];

self.minShowTime=0.0f;

self.graceTime=0.0f;

}

//zhangjian01_可以自定义转速哦,越转越快啊

- (void) rotateWithOptions: (UIViewAnimationOptions) options {

[UIViewanimateWithDuration:0.08f

delay:0.0f

options: options

animations: ^{

bgImageView2.transform=CGAffineTransformRotate(bgImageView2.transform,M_PI/2);

}

completion: ^(BOOLfinished) {

if(finished) {

if(animating) {

[selfrotateWithOptions:UIViewAnimationOptionCurveLinear];

}elseif(options !=UIViewAnimationOptionCurveEaseOut) {

[selfrotateWithOptions:UIViewAnimationOptionCurveEaseOut];

}

}

}];

}

//http://blog.sina.com.cn/resoftios

- (void) startRotate {

if(!animating) {

animating=YES;

[selfrotateWithOptions:UIViewAnimationOptionCurveEaseIn];

}

[selfshowUsingAnimation:animating];

}

- (void) stopRotate{

animating=NO;

self.taskInProgress=NO;

__weaktypeof(self)weakSelf =self;

[UIViewanimateWithDuration:0.5animations:^{

weakSelf.alpha=0;

}completion:^(BOOLfinished) {

//[weakSelf stopRotate];

[weakSelfremoveFromSuperview];

}];

}

- (void) startRotates:(BOOL)animated {

animating= animated;

if(self.graceTime>0.0) {

self.graceTimer= [NSTimerscheduledTimerWithTimeInterval:self.graceTimetarget:self

selector:@selector(handleGraceTimers:)userInfo:nilrepeats:NO];

}

[selfshowUsingAnimation:animating];

}

- (void)handleGraceTimers:(NSTimer*)theTimer {

if(_taskInProgress) {

[selfsetNeedsDisplay];

[selfshowUsingAnimation:animating];

}

}

- (void) stopRotates:(BOOL)animated{

animating= animated;

self.taskInProgress=NO;

if(self.minShowTime>0.0&&_showStarted) {

NSTimeIntervalinterv = [[NSDatedate]timeIntervalSinceDate:_showStarted];

if(interv

self.minShowTimer= [NSTimerscheduledTimerWithTimeInterval:(self.minShowTime- interv)target:self

selector:@selector(handleMinShowTimer:)userInfo:nilrepeats:NO];

return;

}

}

[selfhideUsingAnimation:animating];

}

- (void)handleMinShowTimer:(NSTimer*)theTimer {

[selfhideUsingAnimation:animating];

}

- (void)hideUsingAnimation:(BOOL)animated {

if(animated &&_showStarted) {

__weaktypeof(self)weakSelf =self;

[UIViewanimateWithDuration:0.5animations:^{

weakSelf.alpha=0;

}completion:^(BOOLfinished) {

//[weakSelf stopRotate];

[weakSelfremoveFromSuperview];

}];

[selfdone];

}else{

self.alpha=0.0f;

}

self.showStarted=nil;

}

- (void)done{

if(self.completionBlock) {

self.completionBlock();

self.completionBlock=NULL;

}

}

- (void)showUsingAnimation:(BOOL)animated{

//if (animated) {

[selfrotateWithOptions:UIViewAnimationOptionCurveEaseIn];

//}

self.showStarted= [NSDatedate];

}

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(ZJViewShowHUDCompletionBlock)completion{

dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

[selfshowAnimated:animatedwhileExecutingBlock:blockonQueue:queuecompletionBlock:completion];

}

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue {

[selfshowAnimated:animatedwhileExecutingBlock:blockonQueue:queuecompletionBlock:NULL];

}

- (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue

completionBlock:(ZJViewShowHUDCompletionBlock)completion{

self.taskInProgress=YES;

self.completionBlock= completion;

dispatch_async(queue, ^(void) {

block();

dispatch_async(dispatch_get_main_queue(), ^(void) {

[selfstopRotates:animating];

});

});

[selfstartRotates:animated];

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

}

*/

//http://blog.sina.com.cn/resoftios

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容