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

推荐阅读更多精彩内容