MBProgressHUD在oc、swift專案中的封裝使用
阿新 • • 發佈:2019-02-20
MBProgressHUD在實際專案中用的非常多,不過要處理提示框的地方又要寫很多的程式碼,下面分享一個別人寫的方法
直接移植到專案中就可以用了,OC和swift專案中都通用,使用之前把MBProgressHUD庫移植到專案中
UIViewController+HUD.h
/************************************************************ * * EaseMob CONFIDENTIAL * __________________ * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of EaseMob Technologies. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from EaseMob Technologies. */ #import <UIKit/UIKit.h> @interface UIViewController (HUD) - (void)showHudInView:(UIView *)view hint:(NSString *)hint; - (void)hideHud; - (void)showHint:(NSString *)hint; // 從預設(showHint:)顯示的位置再往上(下)yOffset - (void)showHint:(NSString *)hint yOffset:(float)yOffset; @end
UIViewController+HUD.m
/************************************************************ * * EaseMob CONFIDENTIAL * __________________ * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of EaseMob Technologies. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from EaseMob Technologies. */ #import "UIViewController+HUD.h" #import "MBProgressHUD.h" #import <objc/runtime.h> static const void *HttpRequestHUDKey = &HttpRequestHUDKey; @implementation UIViewController (HUD) - (MBProgressHUD *)HUD{ return objc_getAssociatedObject(self, HttpRequestHUDKey); } - (void)setHUD:(MBProgressHUD *)HUD{ objc_setAssociatedObject(self, HttpRequestHUDKey, HUD, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)showHudInView:(UIView *)view hint:(NSString *)hint{ MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view]; HUD.label.text = hint; [view addSubview:HUD]; [HUD showAnimated:YES]; [self setHUD:HUD]; } - (void)showHint:(NSString *)hint { //顯示提示資訊 UIView *view = [[UIApplication sharedApplication].delegate window]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.userInteractionEnabled = NO; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.label.text = hint; hud.margin = 10.f; CGPoint point = hud.offset; point.y =IS_IPHONE_5?200.f:150.f; hud.offset = point; hud.removeFromSuperViewOnHide = YES; [hud hideAnimated:YES afterDelay:2]; } - (void)showHint:(NSString *)hint yOffset:(float)yOffset { //顯示提示資訊 UIView *view = [[UIApplication sharedApplication].delegate window]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.userInteractionEnabled = NO; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.label.text = hint; hud.margin = 10.f; CGPoint point = hud.offset; point.y =IS_IPHONE_5?200.f:150.f; point.y += yOffset; hud.offset = point; hud.removeFromSuperViewOnHide = YES; [hud hideAnimated:YES afterDelay:2]; } - (void)hideHud{ [[self HUD] hideAnimated:YES]; } @end