YLSingleText.m 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // YLSingleText.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/3/11.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "YLSingleText.h"
  9. #define myRandomColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1]
  10. @interface YLSingleText ()
  11. {
  12. CGFloat _normalSteps;
  13. CGFloat _waveSteps;
  14. BOOL _directionChange;
  15. }
  16. @end
  17. @implementation YLSingleText
  18. + (instancetype)singleTextWithAnimationRange:(CGFloat)animationRange
  19. {
  20. YLSingleText *obj = [[self alloc] init];
  21. obj.animationRange = animationRange;
  22. return obj;
  23. }
  24. - (void)calculateStep
  25. {
  26. _directionChange ? _normalSteps-- : _normalSteps++;
  27. if (_normalSteps > self.animationRange) {
  28. _directionChange = YES;
  29. } else if (_normalSteps < 0) {
  30. _directionChange = NO;
  31. }
  32. }
  33. - (CGFloat)locationWithNormalAnimation
  34. {
  35. [self calculateStep];
  36. return _normalSteps;
  37. }
  38. - (CGFloat)locationWithFirstEnumerate
  39. {
  40. [self calculateStep];
  41. _normalSteps += 1;
  42. if (_normalSteps > self.animationRange) {
  43. _normalSteps = self.animationRange - (_normalSteps - self.animationRange);
  44. }
  45. self.enumerated = YES;
  46. return _normalSteps;
  47. }
  48. - (CGFloat)locationWithWaveAnimationCompletion:(void (^)(void))completion
  49. {
  50. _waveSteps++;
  51. if (_waveSteps > 2 * self.animationRange) {
  52. completion();
  53. return 0;
  54. }
  55. return [self locationWithNormalAnimation];
  56. }
  57. - (void)normalReset
  58. {
  59. self.enumerated = NO;
  60. _directionChange = NO;
  61. _normalSteps = 0;
  62. }
  63. - (void)waveReset
  64. {
  65. _waveSteps = 0;
  66. [self normalReset];
  67. }
  68. @end