PCSModeList.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // PCSModeList.m
  3. // sampleSDK
  4. //
  5. // Created by 王昭威 on 2023/1/15.
  6. //
  7. #import "PCSModeList.h"
  8. #import <Masonry/Masonry.h>
  9. #import "PCSTools.h"
  10. #import "PCSThemeColorManager.h"
  11. #import "OperationNodeProtocol.h"
  12. @interface PCSModeList ()
  13. @property (nonatomic, strong) UIView* list;
  14. @property (nonatomic, assign, readonly) CGFloat space;
  15. @property (nonatomic, strong) NSArray<__kindof UILabel*>* itemViews;
  16. @property (nonatomic, copy) NSArray<id<OperationNodeProtocol>>* items;
  17. @property (nonatomic) CGPoint nextPoint;
  18. @end
  19. @implementation PCSModeList
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. }
  23. return self;
  24. }
  25. - (void)reloadWith:(NSArray<id<OperationNodeProtocol>>*)items{
  26. self.items = items;
  27. self.index = 0;
  28. self.itemViews = [self reloadItemViews];
  29. [self selectItemAt:self.index];
  30. }
  31. - (void)setMode:(SDKCaptureModeIndex)mode{
  32. __block NSUInteger index = NSUIntegerMax;
  33. [self.items enumerateObjectsUsingBlock:^(id<OperationNodeProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  34. if(obj.modeIndex == mode){
  35. index = idx;
  36. *stop = YES;
  37. }
  38. }];
  39. if(_index == index){
  40. return;
  41. }
  42. if(NSUIntegerMax == index){
  43. return;
  44. }
  45. _index = index;
  46. [self updateItemsConstraints];
  47. }
  48. - (SDKCaptureModeIndex)mode{
  49. return (SDKCaptureModeIndex)self.index;
  50. }
  51. - (void)updateItemsConstraints{
  52. __block UILabel* previous = self.itemViews.firstObject;
  53. [self.itemViews enumerateObjectsUsingBlock:^(__kindof UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  54. obj.textColor = UIColor.whiteColor;
  55. [obj mas_remakeConstraints:^(MASConstraintMaker *make) {
  56. if(idx != 0){
  57. make.left.equalTo(previous.mas_right).offset(self.space);
  58. }
  59. make.centerY.equalTo(self);
  60. }];
  61. previous = obj;
  62. }];
  63. [self selectItemAt:self.index];
  64. }
  65. - (CGFloat)space{
  66. return 40;
  67. }
  68. - (void)updateIndexByTanslation:(CGPoint)translation{
  69. NSUInteger oldIndex = self.index;
  70. NSUInteger oldMode = self.selectMode;
  71. if(self.index < self.items.count - 1 && translation.x < 0){
  72. self.index += 1;
  73. }
  74. else if(self.index > 0 && translation.x > 0){
  75. self.index -= 1;
  76. }
  77. if (self.itemViews.count > self.index) {
  78. self.selectMode = self.itemViews[self.index].tag;
  79. }
  80. if (oldIndex != self.index) {
  81. if(self.indexChangedHandler){
  82. if (self.indexChangedHandler(self.selectMode, oldMode, self.index)) {
  83. [self updateItemsConstraints];
  84. } else {
  85. self.index = oldIndex;
  86. self.selectMode = oldMode;
  87. }
  88. }
  89. [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  90. [self layoutIfNeeded];
  91. } completion:^(BOOL finished) {}];
  92. }
  93. }
  94. - (void)selectItemAt: (NSUInteger)index{
  95. if(index >= self.itemViews.count){
  96. return;
  97. }
  98. self.itemViews[index].textColor = [PCSThemeColorManager orange];
  99. [self.itemViews[index] mas_makeConstraints:^(MASConstraintMaker *make) {
  100. make.centerX.equalTo(self);
  101. }];
  102. }
  103. - (NSMutableArray*)reloadItemViews{
  104. NSArray<id<OperationNodeProtocol>>* items = self.items;
  105. __block UILabel* previous = nil;
  106. NSMutableArray* labels = [NSMutableArray array];
  107. [items enumerateObjectsUsingBlock:^(id<OperationNodeProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  108. if (idx == 0) {
  109. self.selectMode = obj.modeIndex;
  110. }
  111. UILabel* label = [[UILabel alloc] init];
  112. label.font = [UIFont systemFontOfSize:13];
  113. [labels addObject:label];
  114. label.translatesAutoresizingMaskIntoConstraints = NO;
  115. label.text = obj.modeItemString;
  116. label.textColor = UIColor.whiteColor;
  117. label.tag = obj.modeIndex;
  118. [self addSubview:label];
  119. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  120. if(previous != nil){
  121. make.left.equalTo(previous.mas_right).offset(self.space);
  122. }
  123. make.centerY.equalTo(self);
  124. }];
  125. previous = label;
  126. }];
  127. return labels;
  128. }
  129. - (void)awakeFromNib{
  130. [super awakeFromNib];
  131. self.backgroundColor = UIColor.clearColor;
  132. UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
  133. [self addGestureRecognizer:tap];
  134. if(self.items == nil || self.items.count == 0){
  135. return;
  136. }
  137. self.index = 0;
  138. self.selectMode = 0;
  139. NSMutableArray* labels = [self reloadItemViews];
  140. self.itemViews = [labels copy];
  141. [self selectItemAt:self.index];
  142. }
  143. - (void)tapHandler: (UITapGestureRecognizer*)gesture{
  144. CGPoint pt = [gesture locationInView:self];
  145. [self.itemViews enumerateObjectsUsingBlock:^(__kindof UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  146. if(CGRectContainsPoint(obj.frame, pt)){
  147. *stop = YES;
  148. NSUInteger oldIndex = self.index;
  149. NSUInteger oldMode = self.selectMode;
  150. self.index = idx;
  151. self.selectMode = obj.tag;
  152. if (oldIndex != self.index) {
  153. if(self.indexChangedHandler){
  154. if (self.indexChangedHandler(self.selectMode, oldMode, self.index)) {
  155. [self updateItemsConstraints];
  156. } else {
  157. self.index = oldIndex;
  158. self.selectMode = oldMode;
  159. }
  160. }
  161. [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  162. [self layoutIfNeeded];
  163. } completion:^(BOOL finished) {}];
  164. }
  165. }
  166. }];
  167. }
  168. @end