ViceLoadingView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // ViceLoadingView.m
  3. // Unity-iPhone
  4. //
  5. // Created by duowan123 on 2022/8/2.
  6. //
  7. /*************************** 获取屏幕 宽度、高度 ***************************/
  8. #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
  9. #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
  10. #define RGBA(a,b,c,d) [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:d]
  11. #define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
  12. #import "ViceLoadingView.h"
  13. #import "MYFactoryManager.h"
  14. @implementation ViceLoadingView
  15. +(instancetype)shanreAnimationView{
  16. static ViceLoadingView * animationView = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. animationView = [ViceLoadingView new];
  20. });
  21. return animationView;
  22. }
  23. -(void)stopAnimation{
  24. // if (self.imageView.isAnimating){
  25. [self.imageView stopAnimating];
  26. [self.animationView removeFromSuperview];
  27. [self.backView removeFromSuperview];
  28. [self.imageView removeFromSuperview];
  29. [self.animationLabel removeFromSuperview];
  30. [self.countButton removeFromSuperview];
  31. // }
  32. [self pauseLayer:self.imageView.layer];
  33. }
  34. //初始化动画
  35. -(void)addAnimation{
  36. if (self.animationView!=nil){
  37. [self stopAnimation];
  38. }
  39. self.animationView = [UIView new];
  40. self.animationView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  41. self.animationView.backgroundColor = RGBA(76, 76, 76, 0.8);
  42. [[UIApplication sharedApplication].keyWindow addSubview:self.animationView];
  43. //
  44. self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 90) / 2,(SCREEN_HEIGHT - 90) / 2, 90, 90)];
  45. self.imageView.image = [MYFactoryManager imageString:@"loading"];
  46. [self.animationView addSubview:self.imageView];
  47. //tip
  48. self.animationLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 90) / 2, (SCREEN_HEIGHT - 90)/2+90+10,180, 40)];
  49. // self.animationLabel.text = @"链接鞋子...";
  50. self.animationLabel.textColor = [UIColor whiteColor];
  51. self.animationLabel.font = [UIFont systemFontOfSize:12];
  52. self.animationLabel.textAlignment = NSTextAlignmentCenter;
  53. self.animationLabel.backgroundColor = [UIColor clearColor];
  54. [self.animationView addSubview:self.animationLabel];
  55. //添加旋转动画
  56. _rotationAnimation = [self rotationDurTime:2 angle:360.f direction:-1 repeatCount:MAXFLOAT];
  57. _rotationAnimation.delegate = self;
  58. _rotationAnimation.removedOnCompletion = NO;
  59. [_rotationAnimation setRepeatCount:HUGE_VALF];
  60. _rotationAnimation.speed = 1;
  61. [self.imageView.layer addAnimation:self.rotationAnimation forKey:@"rotate"];
  62. WS(ws);
  63. //链接超时
  64. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  65. [ws stopAnimation];
  66. });
  67. }
  68. //暂停layer上面的动画
  69. -(void)pauseLayer:(CALayer*)layer{
  70. CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
  71. layer.speed = 0.0;
  72. layer.timeOffset = pausedTime;
  73. }
  74. //周期时间 角度 方向 重复次数
  75. -(CABasicAnimation *)rotationDurTime:(float)durTime angle:(float)angle direction:(int)direction repeatCount:(int)repeatCount{
  76. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
  77. // animation.fromValue = [NSNumber numberWithFloat:0.f];
  78. animation.toValue = [NSNumber numberWithFloat:M_PI];
  79. animation.duration= durTime;
  80. animation.cumulative= YES;//是否累加
  81. animation.removedOnCompletion=NO;
  82. animation.fillMode =kCAFillModeForwards;
  83. animation.autoreverses = NO;//自动逆转
  84. animation.repeatCount = repeatCount;
  85. return animation;
  86. }
  87. @end