RAProgressHUD.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // RAProgressHUD.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/5.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAProgressHUD.h"
  9. static const CGFloat HUDSIZE = 100.0f;
  10. @interface RAProgressHUD ()
  11. @property (nonatomic,strong) CAGradientLayer *gradientLayer;
  12. @property (nonatomic,strong) CAShapeLayer *progressLayer;
  13. @property (nonatomic,strong) UIActivityIndicatorView *activityIndicator;
  14. @property (nonatomic,strong) UILabel *indicatorLabel;
  15. @end
  16. @implementation RAProgressHUD
  17. + (instancetype)showHUDOnView:(UIView *)view {
  18. return [self showHUDOnView:view withTitle:@"loading..."];
  19. }
  20. + (instancetype)showHUDOnView:(UIView *)view withTitle:(NSString *)title {
  21. if (!view) {
  22. return nil;
  23. }
  24. RAProgressHUD *hud = [[RAProgressHUD alloc] init];
  25. hud.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.8];
  26. hud.layer.cornerRadius = 5.0f;
  27. hud.layer.masksToBounds = YES;
  28. [view addSubview:hud];
  29. hud.translatesAutoresizingMaskIntoConstraints = NO;
  30. NSLayoutConstraint *h_center = [NSLayoutConstraint constraintWithItem:hud
  31. attribute:NSLayoutAttributeCenterX
  32. relatedBy:NSLayoutRelationEqual
  33. toItem:view
  34. attribute:NSLayoutAttributeCenterX
  35. multiplier:1
  36. constant:0];
  37. NSLayoutConstraint *v_center = [NSLayoutConstraint constraintWithItem:hud
  38. attribute:NSLayoutAttributeCenterY
  39. relatedBy:NSLayoutRelationEqual
  40. toItem:view
  41. attribute:NSLayoutAttributeCenterY
  42. multiplier:1
  43. constant:0];
  44. NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:hud
  45. attribute:NSLayoutAttributeWidth
  46. relatedBy:NSLayoutRelationEqual
  47. toItem:nil
  48. attribute:NSLayoutAttributeNotAnAttribute
  49. multiplier:0
  50. constant:HUDSIZE];
  51. NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:hud
  52. attribute:NSLayoutAttributeHeight
  53. relatedBy:NSLayoutRelationEqual
  54. toItem:nil
  55. attribute:NSLayoutAttributeNotAnAttribute
  56. multiplier:0
  57. constant:HUDSIZE];
  58. [view addConstraints:@[h_center,v_center,width,height]];
  59. hud.title = title;
  60. hud.indicatorLabel.frame = CGRectMake(0, 70, HUDSIZE, 20);
  61. [hud addSubview:hud.indicatorLabel];
  62. hud.activityIndicator.frame = CGRectMake(20, 5, 60, 60);
  63. [hud addSubview:hud.activityIndicator];
  64. return hud;
  65. }
  66. - (UIActivityIndicatorView *)activityIndicator {
  67. if (!_activityIndicator) {
  68. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  69. [_activityIndicator startAnimating];
  70. }
  71. return _activityIndicator;
  72. }
  73. - (UILabel *)indicatorLabel {
  74. if (!_indicatorLabel) {
  75. _indicatorLabel = [[UILabel alloc] init];
  76. _indicatorLabel.textColor = [UIColor whiteColor];
  77. _indicatorLabel.textAlignment = NSTextAlignmentCenter;
  78. _indicatorLabel.font = [UIFont systemFontOfSize:14.0f];
  79. }
  80. return _indicatorLabel;
  81. }
  82. - (void)dismiss {
  83. [self dismiss:nil];
  84. }
  85. - (void)dismiss:(void(^)(void))completion {
  86. [UIView animateWithDuration:0.25 animations:^{
  87. self.alpha = 0.3;
  88. } completion:^(BOOL finished) {
  89. [self removeFromSuperview];
  90. if (completion) {
  91. completion();
  92. }
  93. }];
  94. }
  95. - (void)setTitle:(NSString *)title {
  96. _title = title;
  97. self.indicatorLabel.text = title;
  98. }
  99. @end