首页 > 代码库 > 开源类库之三(MPProgressHUD)
开源类库之三(MPProgressHUD)
MPProgressHUD是一个非常好用的进度指示器类库,其提供了苹果官方sdk没有提供的progress indicator接口,且提供多种样式,使用方法简便。
首先将类库文件添加到项目中。
使用实例代码如下:
[cpp] view plaincopy
- #import <UIKit/UIKit.h>
- #import "MBProgressHUD.h"
[cpp] view plaincopy
- #import <libkern/OSAtomic.h>
- @interface SampleViewController : UITableViewController <MBProgressHUDDelegate>
- @property (nonatomic, retain) NSCondition* condition;
- @property (nonatomic, retain) MBProgressHUD* hud;
- @end
- static volatile NSInteger WAITING_RESPONSE_FOR_SERVERRESPONSE = 0;
- - (void) popOutMBProgressHUD;
- - (void) selectorForMPProgressHUD;
- - (void) notifyMPProgressHUDToDisappear;
- @implementation SampleViewController
- @synthesize hud = _hud;
- @synthesize condition = _condition;
- - (id) initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder: aDecoder];
- if (self != nil) {
- _hud = nil;
- _condition = [[NSCondition alloc] init];
- }
- return self;
- }
- - (void) dealloc
- {
- [_hud release];
- [_condition release];
- }
- - (void) popOutMBProgressHUD
- {
- MBProgressHUD* tempHud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
- self.hud = tempHud;
- [self.navigationController.view addSubview: tempHud];
- self.hud.dimBackground = YES;
- self.hud.delegate = self;
- self.hud.labelText = @"正在处理";
- [self.hud showWhileExecuting:@selector(selectorForMPProgressHUD) onTarget:self withObject: nil animated:YES];
- [tempHud release];
- }
- - (void) selectorForMPProgressHUD
- {
- OSAtomicCompareAndSwapInt(0,
- 1,
- &WAITING_RESPONSE_FOR_SERVERRESPONSE);
- [self performSelectorInBackground: @selector(tempSelector) withObject: nil];
- [self.condition lock];
- while (OSAtomicCompareAndSwapInt(1,
- 1,
- &WAITING_RESPONSE_FOR_SERVERRESPONSE)) {
- NSDate* timeOutDate = [NSDate dateWithTimeIntervalSinceNow: 5.0f];
- [self.condition waitUntilDate: timeOutDate];
- }
- [self.condition unlock];
- }
- - (void) notifyMPProgressHUDToDisappear
- {
- //通知进度显示hud消失
- [self.condition lock];
- OSAtomicCompareAndSwapInt(1,
- 0,
- &WAITING_RESPONSE_FOR_SERVERRESPONSE);
- [self.condition signal];
- [self.condition unlock];
- }
[cpp] view plaincopy
- - (void)hudWasHidden:(MBProgressHUD *)hud
- {
- // Remove HUD from screen when the HUD was hidded
- [self.hud removeFromSuperview];
- self.hud = nil;
- }
- - (void) tempSelector
- {
- sleep(3.0f);//模拟真实的耗时操作
- [self notifyMPProgressHUDToDisappear];
- }
- @end
开源类库之三(MPProgressHUD)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。