WFCUWaitingAnimationView.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // WFCUWaitingAnimationView.m
  3. // WFChatUIKit
  4. //
  5. // Created by Tom Lee on 2020/2/23.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUWaitingAnimationView.h"
  9. @interface WFCUWaitingAnimationView ()
  10. @property(nonatomic, strong)NSTimer *animatedTimer;
  11. @property(nonatomic, assign)int index;
  12. @property(nonatomic, strong)UIImageView *centerImageView;
  13. @end
  14. @implementation WFCUWaitingAnimationView
  15. /*
  16. // Only override drawRect: if you perform custom drawing.
  17. // An empty implementation adversely affects performance during animation.
  18. - (void)drawRect:(CGRect)rect {
  19. // Drawing code
  20. }
  21. */
  22. - (void)start {
  23. if (!self.animatedTimer) {
  24. self.animatedTimer = [NSTimer scheduledTimerWithTimeInterval:self.animationDuration target:self selector:@selector(setNextImage) userInfo:nil repeats:YES];
  25. [self setNextImage];
  26. }
  27. }
  28. -(void)setNextImage
  29. {
  30. if (!self.animationImages.count) {
  31. return;
  32. }
  33. self.index++;
  34. if (self.index >= self.animationImages.count) {
  35. self.index = 0;
  36. }
  37. self.centerImageView.image = [self.animationImages objectAtIndex:self.index];
  38. }
  39. - (void)stop {
  40. [self.animatedTimer invalidate];
  41. self.animatedTimer = nil;
  42. self.centerImageView.image = nil;
  43. }
  44. - (void)setImage:(UIImage *)image {
  45. self.centerImageView.image = image;
  46. }
  47. - (UIImageView *)centerImageView {
  48. if (!_centerImageView) {
  49. CGFloat width = self.frame.size.width;
  50. _centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(width/4, width/4, width/2, width/2)];
  51. [self addSubview:_centerImageView];
  52. }
  53. return _centerImageView;
  54. }
  55. @end