// // RAProgressHUD.m // Apex And Drivers // // Created by Jack on 2018/6/5. // Copyright © 2018年 USAI. All rights reserved. // #import "RAProgressHUD.h" static const CGFloat HUDSIZE = 100.0f; @interface RAProgressHUD () @property (nonatomic,strong) CAGradientLayer *gradientLayer; @property (nonatomic,strong) CAShapeLayer *progressLayer; @property (nonatomic,strong) UIActivityIndicatorView *activityIndicator; @property (nonatomic,strong) UILabel *indicatorLabel; @end @implementation RAProgressHUD + (instancetype)showHUDOnView:(UIView *)view { return [self showHUDOnView:view withTitle:NSLocalizedString(@"loading...", @"loading...")]; } + (instancetype)showHUDOnView:(UIView *)view withTitle:(NSString *)title { if (!view) { return nil; } RAProgressHUD *hud = [[RAProgressHUD alloc] init]; hud.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.8]; hud.layer.cornerRadius = 5.0f; hud.layer.masksToBounds = YES; [view addSubview:hud]; hud.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *h_center = [NSLayoutConstraint constraintWithItem:hud attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; NSLayoutConstraint *v_center = [NSLayoutConstraint constraintWithItem:hud attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:hud attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:HUDSIZE]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:hud attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:HUDSIZE]; [view addConstraints:@[h_center,v_center,width,height]]; hud.title = title; hud.indicatorLabel.frame = CGRectMake(0, 70, HUDSIZE, 20); [hud addSubview:hud.indicatorLabel]; hud.activityIndicator.frame = CGRectMake(20, 5, 60, 60); [hud addSubview:hud.activityIndicator]; // hud.window.userInteractionEnabled = NO; return hud; } - (UIActivityIndicatorView *)activityIndicator { if (!_activityIndicator) { _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [_activityIndicator startAnimating]; } return _activityIndicator; } - (UILabel *)indicatorLabel { if (!_indicatorLabel) { _indicatorLabel = [[UILabel alloc] init]; _indicatorLabel.textColor = [UIColor whiteColor]; _indicatorLabel.textAlignment = NSTextAlignmentCenter; _indicatorLabel.font = [UIFont systemFontOfSize:14.0f]; } return _indicatorLabel; } - (void)dismiss { [self dismiss:nil]; } - (void)dismiss:(void(^)(void))completion { // self.window.userInteractionEnabled = YES; [UIView animateWithDuration:0.25 animations:^{ self.alpha = 0.3; } completion:^(BOOL finished) { [self removeFromSuperview]; if (completion) { completion(); } }]; } - (void)setTitle:(NSString *)title { _title = title; self.indicatorLabel.text = title; } @end