NIMKitProgressHUD.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // NIMKitProgressHUD.m
  3. // NIMKit
  4. //
  5. // Created by chris on 2017/7/28.
  6. // Copyright © 2017年 NetEase. All rights reserved.
  7. //
  8. #import "NIMKitProgressHUD.h"
  9. #import "UIView+NIM.h"
  10. #import "NIMKit.h"
  11. @interface NIMKitProgressHUD()
  12. @property (nonatomic, strong) CAShapeLayer *indefiniteAnimatedLayer;
  13. @end
  14. @implementation NIMKitProgressHUD
  15. + (instancetype)sharedView
  16. {
  17. static NIMKitProgressHUD *instance = nil;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. instance = [[NIMKitProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 84, 84)];
  21. });
  22. return instance;
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame
  25. {
  26. self = [super initWithFrame:frame];
  27. if (self) {
  28. self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  29. UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  30. UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  31. blurView.frame = self.bounds;
  32. [self.contentView addSubview:blurView];
  33. self.backgroundColor = [UIColor whiteColor];
  34. self.layer.cornerRadius = 14.f;
  35. self.alpha = 0.8;
  36. self.clipsToBounds = YES;
  37. }
  38. return self;
  39. }
  40. + (void)show
  41. {
  42. [[NIMKitProgressHUD sharedView] showInView:[UIApplication sharedApplication].keyWindow];
  43. }
  44. + (void)dismiss
  45. {
  46. dispatch_async(dispatch_get_main_queue(), ^{
  47. [[NIMKitProgressHUD sharedView] removeFromSuperview];
  48. [[NIMKitProgressHUD sharedView].indefiniteAnimatedLayer removeFromSuperlayer];
  49. });
  50. }
  51. - (void)showInView:(UIView *)view
  52. {
  53. [view addSubview:self];
  54. self.center = view.center;
  55. [CATransaction begin];
  56. [CATransaction setDisableActions:YES];
  57. [self.layer addSublayer:self.indefiniteAnimatedLayer];
  58. [CATransaction commit];
  59. }
  60. - (CAShapeLayer*)indefiniteAnimatedLayer {
  61. if(!_indefiniteAnimatedLayer) {
  62. CGFloat strokeThickness = 2.f;
  63. CGFloat radius = 18.f;
  64. CGPoint arcCenter = CGPointMake(radius+strokeThickness/2+5, radius+strokeThickness/2+5);
  65. UIBezierPath* smoothedPath = [UIBezierPath bezierPathWithArcCenter:arcCenter radius:radius startAngle:(CGFloat) (M_PI*3/2) endAngle:(CGFloat) (M_PI/2+M_PI*5) clockwise:YES];
  66. _indefiniteAnimatedLayer = [CAShapeLayer layer];
  67. _indefiniteAnimatedLayer.contentsScale = [[UIScreen mainScreen] scale];
  68. _indefiniteAnimatedLayer.frame = CGRectMake(0.0f, 0.0f, arcCenter.x*2, arcCenter.y*2);
  69. _indefiniteAnimatedLayer.fillColor = [UIColor clearColor].CGColor;
  70. _indefiniteAnimatedLayer.strokeColor = [UIColor blackColor].CGColor;
  71. _indefiniteAnimatedLayer.lineWidth = 2;
  72. _indefiniteAnimatedLayer.lineCap = kCALineCapRound;
  73. _indefiniteAnimatedLayer.lineJoin = kCALineJoinBevel;
  74. _indefiniteAnimatedLayer.path = smoothedPath.CGPath;
  75. CALayer *maskLayer = [CALayer layer];
  76. NSBundle *bundle = [NSBundle bundleForClass:[NIMKitProgressHUD class]];
  77. NSURL *url = [bundle URLForResource:[NIMKit sharedKit].resourceBundleName withExtension:nil];
  78. NSBundle *imageBundle = [NSBundle bundleWithURL:url];
  79. NSString *path = [imageBundle pathForResource:@"bk_angle_mask" ofType:@"png"];
  80. maskLayer.contents = (__bridge id)[[UIImage imageWithContentsOfFile:path] CGImage];
  81. maskLayer.frame = _indefiniteAnimatedLayer.bounds;
  82. _indefiniteAnimatedLayer.mask = maskLayer;
  83. NSTimeInterval animationDuration = 1;
  84. CAMediaTimingFunction *linearCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  85. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  86. animation.fromValue = (id) 0;
  87. animation.toValue = @(M_PI*2);
  88. animation.duration = animationDuration;
  89. animation.timingFunction = linearCurve;
  90. animation.removedOnCompletion = NO;
  91. animation.repeatCount = INFINITY;
  92. animation.fillMode = kCAFillModeForwards;
  93. animation.autoreverses = NO;
  94. [_indefiniteAnimatedLayer.mask addAnimation:animation forKey:@"rotate"];
  95. CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
  96. animationGroup.duration = animationDuration;
  97. animationGroup.repeatCount = INFINITY;
  98. animationGroup.removedOnCompletion = NO;
  99. animationGroup.timingFunction = linearCurve;
  100. CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
  101. strokeStartAnimation.fromValue = @0.015;
  102. strokeStartAnimation.toValue = @0.515;
  103. CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
  104. strokeEndAnimation.fromValue = @0.485;
  105. strokeEndAnimation.toValue = @0.985;
  106. animationGroup.animations = @[strokeStartAnimation, strokeEndAnimation];
  107. [_indefiniteAnimatedLayer addAnimation:animationGroup forKey:@"progress"];
  108. }
  109. return _indefiniteAnimatedLayer;
  110. }
  111. - (void)layoutSubviews
  112. {
  113. [super layoutSubviews];
  114. self.indefiniteAnimatedLayer.position = CGPointMake(self.nim_width * 0.5f, self.nim_height * 0.5f);
  115. }
  116. @end