AccompanyLoadingView.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // AccompanyLoadingView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/8/15.
  6. //
  7. #import "AccompanyLoadingView.h"
  8. #import <Lottie/Lottie.h>
  9. @interface AccompanyLoadingView ()
  10. @property (weak, nonatomic) IBOutlet UIView *loadContainer;
  11. @property (nonatomic, strong) LOTAnimationView *animationView;
  12. @property (nonatomic, strong) NSString *jsonString;
  13. @property (nonatomic, copy) AccompanyLoadingCallback callback;
  14. @end
  15. @implementation AccompanyLoadingView
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. self.jsonString = @"cloud_animation";
  19. }
  20. - (void)loadingCallback:(AccompanyLoadingCallback)callback {
  21. if (callback) {
  22. self.callback = callback;
  23. }
  24. }
  25. + (instancetype)shareInstance {
  26. AccompanyLoadingView *view = [[[NSBundle mainBundle] loadNibNamed:@"AccompanyLoadingView" owner:nil options:nil] firstObject];
  27. return view;
  28. }
  29. - (void)showLoading {
  30. self.layer.opacity = 1.0f;
  31. if (self.animationView.isAnimationPlaying) {
  32. return;
  33. }
  34. if (![self.loadContainer.subviews containsObject:self.animationView]) {
  35. [self.loadContainer addSubview:self.animationView];
  36. [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.right.top.bottom.mas_equalTo(self.loadContainer);
  38. }];
  39. }
  40. [self.animationView play];
  41. }
  42. - (void)stopLoading {
  43. if (self.animationView.isAnimationPlaying) {
  44. [self.animationView stop];
  45. }
  46. [UIView animateWithDuration:1.0f animations:^{
  47. self.layer.opacity = 0.0f;
  48. } completion:^(BOOL finished) {
  49. [self removeFromSuperview];
  50. }];
  51. }
  52. - (IBAction)cancleAction:(id)sender {
  53. if (self.animationView.isAnimationPlaying) {
  54. [self.animationView stop];
  55. }
  56. if (self.callback) {
  57. self.callback();
  58. }
  59. }
  60. - (LOTAnimationView *)animationView {
  61. if (!_animationView) {
  62. _animationView = [LOTAnimationView animationNamed:self.jsonString];
  63. _animationView.contentMode = UIViewContentModeScaleAspectFill;
  64. _animationView.animationSpeed = 1.0;
  65. _animationView.loopAnimation = YES;
  66. }
  67. return _animationView;
  68. }
  69. /*
  70. // Only override drawRect: if you perform custom drawing.
  71. // An empty implementation adversely affects performance during animation.
  72. - (void)drawRect:(CGRect)rect {
  73. // Drawing code
  74. }
  75. */
  76. @end