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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容