LOTAnimatedSwitch.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // LOTAnimatedSwitch.m
  3. // Lottie
  4. //
  5. // Created by brandon_withrow on 8/25/17.
  6. // Copyright © 2017 Airbnb. All rights reserved.
  7. //
  8. #import "LOTAnimatedSwitch.h"
  9. #import "LOTAnimationView.h"
  10. #import "CGGeometry+LOTAdditions.h"
  11. @implementation LOTAnimatedSwitch {
  12. CGFloat _onStartProgress;
  13. CGFloat _onEndProgress;
  14. CGFloat _offStartProgress;
  15. CGFloat _offEndProgress;
  16. CGPoint _touchTrackingStart;
  17. BOOL _on;
  18. BOOL _suppressToggle;
  19. BOOL _toggleToState;
  20. }
  21. /// Convenience method to initialize a control from the Main Bundle by name
  22. + (instancetype _Nonnull)switchNamed:(NSString * _Nonnull)toggleName {
  23. return [self switchNamed:toggleName inBundle:[NSBundle mainBundle]];
  24. }
  25. /// Convenience method to initialize a control from the specified bundle by name
  26. + (instancetype _Nonnull)switchNamed:(NSString * _Nonnull)toggleName inBundle:(NSBundle * _Nonnull)bundle {
  27. LOTComposition *composition = [LOTComposition animationNamed:toggleName inBundle:bundle];
  28. LOTAnimatedSwitch *animatedControl = [[self alloc] initWithFrame:CGRectZero];
  29. if (composition) {
  30. [animatedControl setAnimationComp:composition];
  31. animatedControl.bounds = composition.compBounds;
  32. }
  33. return animatedControl;
  34. }
  35. - (instancetype)initWithFrame:(CGRect)frame {
  36. self = [super initWithFrame:frame];
  37. if (self) {
  38. self.accessibilityHint = NSLocalizedString(@"Double tap to toggle setting.", @"Double tap to toggle setting.");
  39. _onStartProgress = 0;
  40. _onEndProgress = 1;
  41. _offStartProgress = 1;
  42. _offEndProgress = 0;
  43. _on = NO;
  44. [self addTarget:self action:@selector(_toggle) forControlEvents:UIControlEventTouchUpInside];
  45. }
  46. return self;
  47. }
  48. - (void)setAnimationComp:(LOTComposition *)animationComp {
  49. [super setAnimationComp:animationComp];
  50. [self setOn:_on animated:NO];
  51. }
  52. #pragma mark - External Methods
  53. - (void)setProgressRangeForOnState:(CGFloat)fromProgress toProgress:(CGFloat)toProgress {
  54. _onStartProgress = fromProgress;
  55. _onEndProgress = toProgress;
  56. [self setOn:_on animated:NO];
  57. }
  58. - (void)setProgressRangeForOffState:(CGFloat)fromProgress toProgress:(CGFloat)toProgress {
  59. _offStartProgress = fromProgress;
  60. _offEndProgress = toProgress;
  61. [self setOn:_on animated:NO];
  62. }
  63. - (void)setOn:(BOOL)on {
  64. [self setOn:on animated:NO];
  65. }
  66. - (void)setOn:(BOOL)on animated:(BOOL)animated {
  67. _on = on;
  68. CGFloat startProgress = on ? _onStartProgress : _offStartProgress;
  69. CGFloat endProgress = on ? _onEndProgress : _offEndProgress;
  70. CGFloat finalProgress = endProgress;
  71. if (self.animationView.animationProgress < MIN(startProgress, endProgress) ||
  72. self.animationView.animationProgress > MAX(startProgress, endProgress)) {
  73. if (self.animationView.animationProgress != (!_on ? _onEndProgress : _offEndProgress)) {
  74. // Current progress is in the wrong timeline. Switch.
  75. endProgress = on ? _offStartProgress : _onStartProgress;
  76. startProgress = on ? _offEndProgress : _onEndProgress;
  77. }
  78. }
  79. if (finalProgress == self.animationView.animationProgress) {
  80. return;
  81. }
  82. if (animated) {
  83. [self.animationView pause];
  84. [self.animationView playFromProgress:startProgress toProgress:endProgress withCompletion:^(BOOL animationFinished) {
  85. if (animationFinished) {
  86. self.animationView.animationProgress = finalProgress;
  87. }
  88. }];
  89. } else {
  90. self.animationView.animationProgress = endProgress;
  91. }
  92. }
  93. - (NSString *)accessibilityValue {
  94. return self.isOn ? NSLocalizedString(@"On", @"On") : NSLocalizedString(@"Off", @"Off");
  95. }
  96. #pragma mark - Internal Methods
  97. - (void)_toggle {
  98. if (!_suppressToggle) {
  99. [self _toggleAndSendActions];
  100. }
  101. }
  102. - (void)_toggleAndSendActions {
  103. if (self.isEnabled) {
  104. #ifndef TARGET_OS_TV
  105. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
  106. UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
  107. [generator impactOccurred];
  108. }
  109. #endif
  110. [self setOn:!_on animated:YES];
  111. [self sendActionsForControlEvents:UIControlEventValueChanged];
  112. }
  113. }
  114. - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
  115. [super beginTrackingWithTouch:touch withEvent:event];
  116. _suppressToggle = NO;
  117. _touchTrackingStart = [touch locationInView:self];
  118. return YES;
  119. }
  120. - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
  121. BOOL superContinue = [super continueTrackingWithTouch:touch withEvent:event];
  122. if (!_interactiveGesture) {
  123. return superContinue;
  124. }
  125. CGPoint location = [touch locationInView:self];
  126. CGFloat diff = location.x - _touchTrackingStart.x;
  127. if (LOT_PointDistanceFromPoint(_touchTrackingStart, location) > self.bounds.size.width * 0.25) {
  128. // The touch has moved enough to register as its own gesture. Suppress the touch up toggle.
  129. _suppressToggle = YES;
  130. }
  131. #ifdef __IPHONE_11_0
  132. // Xcode 9+
  133. if (@available(iOS 9.0, *)) {
  134. #else
  135. // Xcode 8-
  136. if ([UIView respondsToSelector:@selector(userInterfaceLayoutDirectionForSemanticContentAttribute:)]) {
  137. #endif
  138. if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:self.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft) {
  139. diff = diff * -1;
  140. }
  141. }
  142. if (_on) {
  143. diff = diff * -1;
  144. if (diff <= 0) {
  145. self.animationView.animationProgress = _onEndProgress;
  146. _toggleToState = YES;
  147. } else {
  148. diff = MAX(MIN(self.bounds.size.width, diff), 0);
  149. self.animationView.animationProgress = LOT_RemapValue(diff, 0, self.bounds.size.width, _offStartProgress, _offEndProgress);
  150. _toggleToState = (diff / self.bounds.size.width) > 0.5 ? NO : YES;
  151. }
  152. } else {
  153. if (diff <= 0) {
  154. self.animationView.animationProgress = _offEndProgress;
  155. _toggleToState = NO;
  156. } else {
  157. diff = MAX(MIN(self.bounds.size.width, diff), 0);
  158. self.animationView.animationProgress = LOT_RemapValue(diff, 0, self.bounds.size.width, _onStartProgress, _onEndProgress);
  159. _toggleToState = (diff / self.bounds.size.width) > 0.5 ? YES : NO;
  160. }
  161. }
  162. return YES;
  163. }
  164. - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
  165. [super endTrackingWithTouch:touch withEvent:event];
  166. if (!_interactiveGesture) {
  167. return;
  168. }
  169. if (_suppressToggle) {
  170. if (_toggleToState != _on) {
  171. [self _toggleAndSendActions];
  172. } else {
  173. [self setOn:_toggleToState animated:YES];
  174. }
  175. }
  176. }
  177. @end