PCSModeList.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. make.width.mas_offset(60);
  61. make.height.mas_offset(44);
  62. }];
  63. previous = obj;
  64. }];
  65. [self selectItemAt:self.index];
  66. }
  67. - (CGFloat)space{
  68. return 10;
  69. }
  70. - (void)updateIndexByTanslation:(CGPoint)translation{
  71. NSUInteger oldIndex = self.index;
  72. NSUInteger oldMode = self.selectMode;
  73. if(self.index < self.items.count - 1 && translation.x < 0){
  74. self.index += 1;
  75. }
  76. else if(self.index > 0 && translation.x > 0){
  77. self.index -= 1;
  78. }
  79. if (self.itemViews.count > self.index) {
  80. self.selectMode = self.itemViews[self.index].tag;
  81. }
  82. if (oldIndex != self.index) {
  83. if(self.indexChangedHandler){
  84. if (self.indexChangedHandler(self.selectMode, oldMode, self.index)) {
  85. [self updateItemsConstraints];
  86. } else {
  87. self.index = oldIndex;
  88. self.selectMode = oldMode;
  89. }
  90. }
  91. [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  92. [self layoutIfNeeded];
  93. } completion:^(BOOL finished) {}];
  94. }
  95. }
  96. - (void)selectItemAt: (NSUInteger)index{
  97. if(index >= self.itemViews.count){
  98. return;
  99. }
  100. self.itemViews[index].textColor = [PCSThemeColorManager orange];
  101. [self.itemViews[index] mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.centerX.equalTo(self);
  103. }];
  104. }
  105. - (NSMutableArray*)reloadItemViews{
  106. NSArray<id<OperationNodeProtocol>>* items = self.items;
  107. __block UILabel* previous = nil;
  108. NSMutableArray* labels = [NSMutableArray array];
  109. [items enumerateObjectsUsingBlock:^(id<OperationNodeProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  110. if (idx == 0) {
  111. self.selectMode = obj.modeIndex;
  112. }
  113. UILabel* label = [[UILabel alloc] init];
  114. label.font = [UIFont systemFontOfSize:13];
  115. [labels addObject:label];
  116. label.textAlignment = NSTextAlignmentCenter;
  117. label.translatesAutoresizingMaskIntoConstraints = NO;
  118. label.text = obj.modeItemString;
  119. label.textColor = UIColor.whiteColor;
  120. label.tag = obj.modeIndex;
  121. [self addSubview:label];
  122. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  123. if(previous != nil){
  124. make.left.equalTo(previous.mas_right).offset(self.space);
  125. }
  126. make.centerY.equalTo(self);
  127. make.width.mas_offset(60);
  128. make.height.mas_offset(44);
  129. }];
  130. previous = label;
  131. }];
  132. return labels;
  133. }
  134. - (void)awakeFromNib{
  135. [super awakeFromNib];
  136. self.backgroundColor = UIColor.clearColor;
  137. UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
  138. [self addGestureRecognizer:tap];
  139. if(self.items == nil || self.items.count == 0){
  140. return;
  141. }
  142. self.index = 0;
  143. self.selectMode = 0;
  144. NSMutableArray* labels = [self reloadItemViews];
  145. self.itemViews = [labels copy];
  146. [self selectItemAt:self.index];
  147. }
  148. - (void)tapHandler: (UITapGestureRecognizer*)gesture{
  149. CGPoint pt = [gesture locationInView:self];
  150. [self.itemViews enumerateObjectsUsingBlock:^(__kindof UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  151. if(CGRectContainsPoint(obj.frame, pt)){
  152. *stop = YES;
  153. NSUInteger oldIndex = self.index;
  154. NSUInteger oldMode = self.selectMode;
  155. self.index = idx;
  156. self.selectMode = obj.tag;
  157. if (oldIndex != self.index) {
  158. if(self.indexChangedHandler){
  159. if (self.indexChangedHandler(self.selectMode, oldMode, self.index)) {
  160. [self updateItemsConstraints];
  161. } else {
  162. self.index = oldIndex;
  163. self.selectMode = oldMode;
  164. }
  165. }
  166. [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  167. [self layoutIfNeeded];
  168. } completion:^(BOOL finished) {}];
  169. }
  170. }
  171. }];
  172. }
  173. @end