PCSModeList.m 6.4 KB

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