自定义超轻量级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 设置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 设置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 设置各个控件的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 根据文字返回动态的宽度和高度
- (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.动画重复次数
    [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的地方,请各位大神多多指教或拍砖。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,269评论 25 708
  • 马上,就要迎来新的一年了。 在过去的2016年,你是活了365天?还是活了1天,重复了364遍? 告别过去一年所有...
    赣B七七阅读 183评论 0 0
  • 一、简介 Android6.0权限简记中提到了普通权限请求的相关操作,现在简单介绍Rx下权限的申请使用方法。 Rx...
    one_mighty阅读 2,007评论 0 0
  • 总是在某一个时刻,悲伤的情绪翻滚而至,找不到理由,也没有出口。 雨落的时候,叶子飘落的时候,日升日落的时候,下雪的...
    繁叶日光阅读 168评论 0 1
  • (感觉这章的内容很水,还是看about face4吧) 细化在设计过程中的位置: 7.1约束 在这个环节中,约束显...
    友交互阅读 958评论 0 1