LOTAnimatedControl.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // LOTAnimatedControl.m
  3. // Lottie
  4. //
  5. // Created by brandon_withrow on 8/25/17.
  6. // Copyright © 2017 Airbnb. All rights reserved.
  7. //
  8. #import "LOTAnimatedControl.h"
  9. #import "LOTAnimationView_Internal.h"
  10. @implementation LOTAnimatedControl {
  11. UIControlState _priorState;
  12. NSMutableDictionary *_layerMap;
  13. }
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. [self _commonInit];
  18. }
  19. return self;
  20. }
  21. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  22. self = [super initWithCoder:aDecoder];
  23. if (self) {
  24. [self _commonInit];
  25. }
  26. return self;
  27. }
  28. - (void)_commonInit {
  29. _animationView = [[LOTAnimationView alloc] init];
  30. _animationView.contentMode = UIViewContentModeScaleAspectFit;
  31. _animationView.userInteractionEnabled = NO;
  32. [self addSubview:_animationView];
  33. _layerMap = [NSMutableDictionary dictionary];
  34. }
  35. - (LOTComposition *)animationComp {
  36. return _animationView.sceneModel;
  37. }
  38. - (void)setAnimationComp:(LOTComposition *)animationComp {
  39. [_animationView setSceneModel:animationComp];
  40. [self checkStateChangedAndUpdate:YES];
  41. }
  42. - (void)setLayerName:(NSString * _Nonnull)layerName forState:(UIControlState)state {
  43. _layerMap[@(state)] = layerName;
  44. [self checkStateChangedAndUpdate:YES];
  45. }
  46. #pragma mark - Setter Overrides
  47. - (void)setEnabled:(BOOL)enabled {
  48. _priorState = self.state;
  49. [super setEnabled:enabled];
  50. [self checkStateChangedAndUpdate:NO];
  51. }
  52. - (void)setSelected:(BOOL)selected {
  53. _priorState = self.state;
  54. [super setSelected:selected];
  55. [self checkStateChangedAndUpdate:NO];
  56. }
  57. - (void)setHighlighted:(BOOL)highlighted {
  58. _priorState = self.state;
  59. [super setHighlighted:highlighted];
  60. [self checkStateChangedAndUpdate:NO];
  61. }
  62. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  63. _priorState = self.state;
  64. [super touchesBegan:touches withEvent:event];
  65. [self checkStateChangedAndUpdate:NO];
  66. }
  67. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  68. _priorState = self.state;
  69. [super touchesMoved:touches withEvent:event];
  70. [self checkStateChangedAndUpdate:NO];
  71. }
  72. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  73. _priorState = self.state;
  74. [super touchesEnded:touches withEvent:event];
  75. [self checkStateChangedAndUpdate:NO];
  76. }
  77. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  78. _priorState = self.state;
  79. [super touchesCancelled:touches withEvent:event];
  80. [self checkStateChangedAndUpdate:NO];
  81. }
  82. - (CGSize)intrinsicContentSize {
  83. return _animationView.intrinsicContentSize;
  84. }
  85. - (void)layoutSubviews {
  86. [super layoutSubviews];
  87. _animationView.frame = self.bounds;
  88. }
  89. - (UIAccessibilityTraits)accessibilityTraits {
  90. return UIAccessibilityTraitButton;
  91. }
  92. - (BOOL)isAccessibilityElement
  93. {
  94. return YES;
  95. }
  96. #pragma mark - Private interface implementation
  97. - (void)checkStateChangedAndUpdate:(BOOL)forceUpdate {
  98. if (self.state == _priorState && !forceUpdate) {
  99. return;
  100. }
  101. _priorState = self.state;
  102. NSString *name = _layerMap[@(self.state)];
  103. if (!name) {
  104. return;
  105. }
  106. CALayer *layer = [_animationView layerForKey:name];
  107. if (!layer) {
  108. return;
  109. }
  110. for (CALayer *child in [_animationView compositionLayers]) {
  111. child.hidden = YES;
  112. }
  113. layer.hidden = NO;
  114. }
  115. @end