自定義超輕量級HUD

第一部分:ZProgressView

#import <UIKit/UIKit.h>
typedef NS_OPTIONS (NSInteger ,ZProgressHUDType)
{
    ZProgressHUDTypeLoading = 0,
    ZProgressHUDTypeSuccess = 1,
    ZProgressHUDTypeError   = 2,
    ZProgressHUDTypeMessage = 3
};
@interface ZProgressView : UIView
@property (nonatomic,strong) NSString *message;
@property (nonatomic,assign) ZProgressHUDType hudViewType;
- (instancetype)init;
- (void)hudShowWithType:(ZProgressHUDType)hudViewType message:(NSString *)message;
- (void)show;
- (void)dismiss;
@end

#import "ZProgressView.h"
#import "ZCZLabel.h"
#import "UIColor+Hexadecimal.h"

#define FONT_SIZE 14
#define HUD_WIDTH 200
#define HUD_HEIGHT 50
#define ICON_WIDTH 50

@interface ZProgressView ()
@property (nonatomic,strong) UIImageView *icon;
@property (nonatomic,strong) ZCZLabel *tipsLabel;
@end

@implementation ZProgressView

- (UIImageView *)icon
{
    if (_icon == nil)
    {
        _icon = [[UIImageView alloc]init];
        _icon.frame = CGRectMake(0, 0, 40, 40);
    }
    return _icon;
}

- (ZCZLabel *)tipsLabel
{
    if (_tipsLabel == nil)
    {
        _tipsLabel = [[ZCZLabel alloc]init];
        _tipsLabel.numberOfLines = 0;
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        _tipsLabel.textColor = [UIColor whiteColor];
        _tipsLabel.edgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    }
    return _tipsLabel;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

#pragma mark 設(shè)置self的layer
- (void)resetLayer
{
    self.layer.shadowColor      = [UIColor blackColor].CGColor;
    self.layer.shadowOffset     = CGSizeMake(0, 0);
    self.layer.shadowRadius     = 5;
    self.layer.cornerRadius     = 5;
    self.layer.shadowOpacity    = 1;
}

#pragma mark 設(shè)置type
- (void)hudShowWithType:(ZProgressHUDType)hudViewType message:(NSString *)message
{
    [self resetFrameWithMessage:message];
    [self resetLayer];

    switch (hudViewType) {
        case ZProgressHUDTypeLoading:
        {
            [self showLoadingAnimation];
            self.icon.image = [UIImage imageNamed:@"loading"];
        }
            break;
        case ZProgressHUDTypeSuccess:
        {
            [self.icon.layer removeAllAnimations];
            self.icon.image = [UIImage imageNamed:@"success (3)"];
        }
            break;
        case ZProgressHUDTypeError:
        {
            [self.icon.layer removeAllAnimations];
            self.icon.image = [UIImage imageNamed:@"error (2)"];
        }
            break;
        case ZProgressHUDTypeMessage:
        {
            [self.icon.layer removeAllAnimations];
            self.icon.image = [UIImage imageNamed:@"message (2)"];
        }
            break;
        default:
            break;
    }
}


#pragma mark 設(shè)置各個控件的frame
- (void)resetFrameWithMessage:(NSString *)message
{
    CGSize labelSize = [self getTheRealHeightAndWidthWithMessage:message];
    if (labelSize.height <= HUD_WIDTH)
    {
        if (self.tipsLabel.numberOfLines > 1)
        {
            self.tipsLabel.frame = CGRectMake(ICON_WIDTH, 0, HUD_WIDTH, labelSize.height);
            self.tipsLabel.center = CGPointMake(ICON_WIDTH + HUD_WIDTH/2, 25);
            self.icon.center = CGPointMake(25, 25);
            self.frame = CGRectMake(0, 0, HUD_WIDTH + ICON_WIDTH, HUD_HEIGHT);
        }
        else
        {
            self.tipsLabel.frame = CGRectMake(ICON_WIDTH, 0, HUD_WIDTH, labelSize.height);
            self.tipsLabel.center =CGPointMake(ICON_WIDTH + labelSize.width/2, 25);
            self.icon.center = CGPointMake(25, 25);
            self.frame = CGRectMake(0, 0, labelSize.width + ICON_WIDTH, HUD_HEIGHT);
        }
    }
    else
    {
        self.tipsLabel.frame = CGRectMake(ICON_WIDTH, 0, HUD_WIDTH, labelSize.height);
        self.tipsLabel.center = CGPointMake(ICON_WIDTH + labelSize.width/2, labelSize.height/2);
        self.icon.center = CGPointMake(25, labelSize.height/2);
        self.frame = CGRectMake(0, 0, labelSize.width + ICON_WIDTH, labelSize.height);
    }


    self.tipsLabel.text = message;
    [self addSubview:self.tipsLabel];
    [self addSubview:self.icon];
    self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
    self.backgroundColor = [UIColor colorWithWhite:0.5f alpha:0.5f];

}

#pragma mark 根據(jù)文字返回動態(tài)的寬度和高度
- (CGSize )getTheRealHeightAndWidthWithMessage:(NSString *)message
{
    CGSize labelSize        = [message boundingRectWithSize:CGSizeMake(HUD_WIDTH - (self.tipsLabel.edgeInsets.left + self.tipsLabel.edgeInsets.right) , MAXFLOAT)
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]}
                                                    context:nil].size;
    CGFloat realHeight      = (HUD_WIDTH  * (labelSize.height + self.tipsLabel.edgeInsets.top + self.tipsLabel.edgeInsets.bottom))/HUD_WIDTH;
    CGFloat realWidth = labelSize.width + self.tipsLabel.edgeInsets.left + self.tipsLabel.edgeInsets.right;
    CGSize size = CGSizeMake(realWidth, realHeight);
    return size;
}

#pragma mark 動畫
- (void)showLoadingAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    [animation setToValue:@(M_PI * 2)];
    [animation setRepeatDuration:MAXFLOAT];
    [animation setDuration:1.0f];
    [animation setRemovedOnCompletion:NO];
    [self.icon.layer addAnimation:animation forKey:@"animationKeyOne"];
}

- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.layer.hidden = NO;
    self.alpha = 1.0f;

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [animation setFromValue:@(1.0f)];//1.縮放的開始值
    [animation setToValue:@(0.97f)];//2.所要縮放到的值
    [animation setAutoreverses:YES];//3.是否原路返回
    [animation setDuration:0.618f];//4.動畫時長
    [animation setRepeatDuration:MAXFLOAT];//5.動畫重復(fù)次數(shù)
    [self.layer addAnimation:animation forKey:@"show"];
    [animation setRemovedOnCompletion:YES];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}
@end

第二部分:ZProgressHUD

#import <Foundation/Foundation.h>
#import "ZProgressView.h"
@interface ZProgressHUD : NSObject

@property (nonatomic,strong) ZProgressView *zProgressView;
@property (nonatomic,assign) ZProgressHUDType type;
+ (ZProgressHUD *)shareManager;

- (void)showWithMessage:(NSString *)message type:(ZProgressHUDType)type;
- (void)dismissWithTime:(NSInteger )time;

@end

#import "ZProgressHUD.h"

@implementation ZProgressHUD

+ (ZProgressHUD *)shareManager
{
    static ZProgressHUD *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[ZProgressHUD alloc]init];
        shareManager.zProgressView = [[ZProgressView alloc]init];
        [[UIApplication sharedApplication].keyWindow addSubview:shareManager.zProgressView];
    });
    return shareManager;
}

- (void)showWithMessage:(NSString *)message type:(ZProgressHUDType)type
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.zProgressView show];
        [self.zProgressView hudShowWithType:type message:message];
    });
}
- (void)dismissWithTime:(NSInteger )time
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.zProgressView dismiss];
        });
    });
}

@end

第三部分:效果圖

QQ20170601-144538.gif
代碼不足或太low的地方,請各位大神多多指教或拍磚。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谒臼,一起剝皮案震驚了整個濱河市种玛,隨后出現(xiàn)的幾起案子尔当,更是在濱河造成了極大的恐慌配喳,老刑警劉巖识补,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件族淮,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)祝辣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進(jìn)店門贴妻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蝙斜,你說我怎么就攤上這事名惩。” “怎么了孕荠?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵娩鹉,是天一觀的道長。 經(jīng)常有香客問我稚伍,道長弯予,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任个曙,我火速辦了婚禮熙涤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘困檩。我一直安慰自己祠挫,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布悼沿。 她就那樣靜靜地躺著等舔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪糟趾。 梳的紋絲不亂的頭發(fā)上慌植,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天,我揣著相機(jī)與錄音义郑,去河邊找鬼蝶柿。 笑死,一個胖子當(dāng)著我的面吹牛非驮,可吹牛的內(nèi)容都是我干的交汤。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼劫笙,長吁一口氣:“原來是場噩夢啊……” “哼芙扎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起填大,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤戒洼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后允华,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體圈浇,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡寥掐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了磷蜀。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片召耘。...
    茶點故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蠕搜,靈堂內(nèi)的尸體忽然破棺而出怎茫,到底是詐尸還是另有隱情收壕,我是刑警寧澤妓灌,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站蜜宪,受9級特大地震影響虫埂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜圃验,卻給世界環(huán)境...
    茶點故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一掉伏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧澳窑,春花似錦斧散、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至麻裁,卻和暖如春箍镜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背煎源。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工色迂, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人手销。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓歇僧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親锋拖。 傳聞我的和親對象是個殘疾皇子馏慨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,876評論 2 361

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,331評論 25 707
  • 馬上写隶,就要迎來新的一年了。 在過去的2016年讲仰,你是活了365天慕趴?還是活了1天,重復(fù)了364遍? 告別過去一年所有...
    贛B七七閱讀 171評論 0 0
  • 一冕房、簡介 Android6.0權(quán)限簡記中提到了普通權(quán)限請求的相關(guān)操作躏啰,現(xiàn)在簡單介紹Rx下權(quán)限的申請使用方法。 Rx...
    one_mighty閱讀 1,986評論 0 0
  • 總是在某一個時刻耙册,悲傷的情緒翻滾而至给僵,找不到理由,也沒有出口详拙。 雨落的時候帝际,葉子飄落的時候,日升日落的時候饶辙,下雪的...
    繁葉日光閱讀 156評論 0 1
  • (感覺這章的內(nèi)容很水蹲诀,還是看about face4吧) 細(xì)化在設(shè)計過程中的位置: 7.1約束 在這個環(huán)節(jié)中,約束顯...
    友交互閱讀 945評論 0 1