MBProgressHUD 기반 패키지

여기에 소스 코드 분석 이 있 습 니 다.http://www.jianshu.com/p/485b8d75ccd4
원본 노트 --- MBProgressHUD 는 번 거 롭 지 않 습 니 다.
설명: 이 글 은 주로 자신 이 개발 할 때 찾 아 보고 사용 하 며 많은 자 료 를 참고 했다.
1. 단일 확장:
레 퍼 런 스https://github.com/hungryBoy/alertView;
#import 
#import "MBProgressHUD.h"

static NSString *const kLoadingMessage = @"   ";
static CGFloat const   kShowTime  = 2.0f;

@interface MBManager : NSObject
/**
 *          ,   YES,  PS:   showPermanentAlert:(NSString *) alert   showLoading     
 */
@property (nonatomic, assign) BOOL isShowGloomy;
/**
 *    “   ”,   ,        kLoadingMessage    
 */
+ (void) showLoading;
/**
 *            ,    
 *
 *  @param alert     
 */
+ (void) showPermanentAlert:(NSString *) alert;
/**
 *          ,  2  ,       kShowTime
 *
 *  @param alert     
 */
+ (void) showBriefAlert:(NSString *) alert;
/**
 *    alert
 */
+(void)hideAlert;
///**
// *           ,       
// *
// *  @param imageName        ,   37 x 37     
// *  @param title         
// */
//+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title;

/***************************************
 *                                     *
 *               ,        *
 *                                     *
 ***************************************
 */

/**
 *          view 
 *
 *  @param message    
 *  @param view         view
 */
+ (void) showBriefMessage:(NSString *) message InView:(UIView *) view;
/**
 *       (             hideAlert        )    view 
 *
 *  @param message    
 *  @param view         view
 */
+ (void) showPermanentMessage:(NSString *)message InView:(UIView *) view;
/**
 *         view 
 *
 *  @param view      view
 */
+ (void) showLoadingInView:(UIView *) view;
/**
 *           ,       
 *
 *  @param imageName        ,   37 x 37     
 *  @param title         
 *  @param view          view
 */
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title inView:(UIView *)view;

@end

특성
1. MBProgressHUD 에 대한 패키지, 호출 은 코드 한 마디 만 필요 합 니 다.
2. 제스처 기능 을 추가 하고 화면 을 터치 하면 알림 상 자 를 취소 할 수 있 습 니 다.
#import "MBManager.h"

#define kScreen_height  [[UIScreen mainScreen] bounds].size.height
#define kScreen_width   [[UIScreen mainScreen] bounds].size.width

@interface MBManager ()
{
    UITapGestureRecognizer *tap;
}
@end

@implementation MBManager
UIView *bottomView;
static MBManager *hudManager = nil;
UIView *hudAddedView;

#pragma mark -    
-(instancetype)init{
    if (self = [super init]) {
        [self initBackView];
        self.isShowGloomy = YES;
    }
    return self;
}
#pragma mark -        
-(void)initBackView{
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)];
    bottomView.backgroundColor = [UIColor blackColor];
    bottomView.alpha = 0.5;
    bottomView.hidden = YES;
}
#pragma mark -   
+(instancetype )shareManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hudManager = [[self alloc] init];
    });
    return hudManager;
}
#pragma mark -      
+ (void) showBriefMessage:(NSString *) message InView:(UIView *) view{
    hudAddedView = view;
    [self shareManager];
    if (view == nil) {
        view = [[UIApplication sharedApplication] windows].lastObject;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    hud.mode = MBProgressHUDModeText;
    hud.margin = 10.f;
    //HUD.yOffset = 200;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:kShowTime];
    [hudManager addGestureInView:view];
}
#pragma mark -        
+ (void) showPermanentMessage:(NSString *)message InView:(UIView *) view{
    hudAddedView = view;
    [self shareManager];
    if (view == nil) {
        view = [[UIApplication sharedApplication] windows].lastObject;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeCustomView;
    if (hudManager.isShowGloomy) {
        //     view  botomView frame   view   
        if (hudAddedView) {
            bottomView.frame = CGRectMake(0, 0, hudAddedView.frame.size.width, hudAddedView.frame.size.height);
        }
        [view addSubview:bottomView];
        [hudManager showBackView];
    }
    [view bringSubviewToFront:hud];
    [hudManager addGestureInView:view];
}
#pragma mark -        
+ (void) showLoadingInView:(UIView *) view{
    hudAddedView = view;
    [self shareManager];
    if (view == nil) {
        view = [[UIApplication sharedApplication] windows].lastObject;
    }
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.labelText = kLoadingMessage;
    hud.removeFromSuperViewOnHide = YES;
    if (hudManager.isShowGloomy) {
        //     view  botomView frame   view   
        if (hudAddedView) {
            bottomView.frame = CGRectMake(0, 0, hudAddedView.frame.size.width, hudAddedView.frame.size.height);
        }
        [view addSubview:bottomView];
        [hudManager showBackView];
    }
    [view addSubview:hud];
    [hud show:YES];
    [hudManager addGestureInView:view];
}
+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title inView:(UIView *)view{
    hudAddedView = view;
    [self shareManager];
    if (view == nil) {
        view = [[UIApplication sharedApplication]windows].lastObject;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    UIImageView *littleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
    littleView.image = [UIImage imageNamed:imageName];
    hud.customView = littleView;
    hud.removeFromSuperViewOnHide = YES;
    hud.animationType = MBProgressHUDAnimationZoom;
    hud.labelText = title;
    hud.mode = MBProgressHUDModeCustomView;
    [hud show:YES];
    [hud hide:YES afterDelay:kShowTime];
    [hudManager addGestureInView:view];
}
#pragma mark -     
+(void)showLoading{
    [self showLoadingInView:nil];
}
+(void)showBriefAlert:(NSString *)alert{
    [self showBriefMessage:alert InView:nil];
}
+(void)showPermanentAlert:(NSString *)alert{
    [self showPermanentMessage:alert InView:nil];
}
//+(void)showAlertWithCustomImage:(NSString *)imageName title:(NSString *)title{
//    [self showAlertWithCustomImage:imageName title:title inView:nil];
//}
#pragma mark -      
+(void)hideAlert{
    [hudManager hideBackView];
    UIView *view ;
    if (hudAddedView) {
        view = hudAddedView;
    }else{
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    [self hideHUDForView:view];
}
+ (void)hideHUDForView:(UIView *)view
{
    [self hideHUDForView:view animated:YES];
}
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
    MBProgressHUD *hud = [self HUDForView:view];
    if (hud != nil) {
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:animated];
        return YES;
    }
    return NO;
}
+ (MBProgressHUD *)HUDForView:(UIView *)view {
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            return (MBProgressHUD *)subview;
        }
    }
    return nil;
}
#pragma mark -     
-(void)showBackView{
    bottomView.hidden = NO;
}
-(void)hideBackView{
    bottomView.hidden = YES;
    [tap removeTarget:nil action:nil];
    bottomView.frame = CGRectMake(0, 0, kScreen_width, kScreen_height);
}

#pragma mark -     ,          
-(void)addGestureInView:(UIView *)view{
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTheScreen)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];

}
#pragma mark -    

-(void)tapTheScreen{
    NSLog(@"    ");
    [hudManager hideBackView];
    [tap removeTarget:nil action:nil];
    [MBManager hideAlert];
}
#pragma mark -       
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[MBProgressHUD class]]) {
        return YES;
    }else{
        return NO;
    }
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}

@end

좋은 웹페이지 즐겨찾기