iOS開(kāi)發(fā)之-仿支付寶加載框(類似MBProgressHUD)

在項(xiàng)目實(shí)際的開(kāi)發(fā)中,MBProgressHUD有時(shí)候不能夠滿足項(xiàng)目需求耸携,所有有時(shí)候需要自己去定義。但在開(kāi)發(fā)中也要主要封裝中辕翰,要高內(nèi)聚低耦合夺衍。以下是,仿支付寶的加載框樣式喜命,希望能夠幫助大家沟沙。能給予好評(píng)。喜歡的??可以加個(gè)關(guān)注渊抄,成為粉絲尝胆,互動(dòng)學(xué)習(xí)。圖片可以到https://github.com/zhangjianios/ZJViewProgressHUD去下載护桦。效果圖:直接下來(lái)含衔,顏色背景自己去定義把,都是可以自己定義的哦二庵。


//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;

//開(kāi)始加載動(dòng)畫

- (void) startRotates:(BOOL)animated;

- (void) startRotate;

//停止加載動(dòng)畫

- (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{

/*創(chuàng)建灰色背景*/

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];

///*創(chuàng)建灰色背景*/

//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_可以自定義轉(zhuǎn)速哦,越轉(zhuǎn)越快啊

- (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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末贪染,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子催享,更是在濱河造成了極大的恐慌杭隙,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件因妙,死亡現(xiàn)場(chǎng)離奇詭異痰憎,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)攀涵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門铣耘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人以故,你說(shuō)我怎么就攤上這事蜗细。” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵炉媒,是天一觀的道長(zhǎng)踪区。 經(jīng)常有香客問(wèn)我,道長(zhǎng)吊骤,這世上最難降的妖魔是什么缎岗? 我笑而不...
    開(kāi)封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮白粉,結(jié)果婚禮上密强,老公的妹妹穿的比我還像新娘。我一直安慰自己蜗元,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布系冗。 她就那樣靜靜地躺著奕扣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掌敬。 梳的紋絲不亂的頭發(fā)上惯豆,一...
    開(kāi)封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音奔害,去河邊找鬼楷兽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛华临,可吹牛的內(nèi)容都是我干的芯杀。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼雅潭,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼揭厚!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起扶供,我...
    開(kāi)封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤筛圆,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后椿浓,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體太援,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年扳碍,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了提岔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡左腔,死狀恐怖唧垦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情液样,我是刑警寧澤振亮,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布巧还,位于F島的核電站,受9級(jí)特大地震影響坊秸,放射性物質(zhì)發(fā)生泄漏麸祷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一褒搔、第九天 我趴在偏房一處隱蔽的房頂上張望阶牍。 院中可真熱鬧,春花似錦星瘾、人聲如沸走孽。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)磕瓷。三九已至,卻和暖如春念逞,著一層夾襖步出監(jiān)牢的瞬間困食,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工翎承, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留硕盹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓叨咖,卻偏偏與公主長(zhǎng)得像瘩例,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子芒澜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容