Bez popisu

FKPreferScrollView.m 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // FKPreferScrollView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/5/11.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKPreferScrollView.h"
  9. #import "FKPreferButton.h"
  10. #import "FKPreferItem.h"
  11. #define PREFER_ITEM_MARGIN 29
  12. #define PREFER_ITEM_HEIGHT 35
  13. @interface FKPreferScrollView ()
  14. @property (nonatomic, strong) NSMutableArray *itemViewArrayM;
  15. @end
  16. @implementation FKPreferScrollView
  17. - (instancetype)initWithIndexModelArray:(NSArray *)modelArray{
  18. if (self = [super init]) {
  19. _modelArray = modelArray;
  20. [self rebuildSubviews];
  21. }
  22. return self;
  23. }
  24. - (void)rebuildSubviews{
  25. if (self.modelArray.count == 0) return;
  26. [self removeAllSubviews];
  27. [self resetAllSelectedState];
  28. [self prepareEnoughItemView];
  29. NSArray *lineGroupArray = [self groupForLineWithModelArray:self.modelArray];
  30. [self holdContentSizeForLine:lineGroupArray.count];
  31. NSUInteger index = 0;
  32. UIView *uperView = nil;
  33. for (NSArray *lineArray in lineGroupArray) {
  34. FKPreferButton *frontBtn = nil;
  35. for (NSInteger i = 0; i < lineArray.count; i++) {
  36. FKPreferButton *button = self.itemViewArrayM[index];
  37. if (!button) continue;
  38. [self addSubview:button];
  39. FKPreferItem *item = lineArray[i];
  40. CGFloat buttonW = [FKPreferButton widthForTitle:item.name];
  41. button.title = item.name;
  42. button.selected = item.selected;
  43. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  44. if (i == 0){
  45. make.left.equalTo(self).offset(15);
  46. if (uperView){
  47. make.top.equalTo(uperView.mas_bottom).offset(PREFER_ITEM_MARGIN);
  48. }else{
  49. make.top.equalTo(self).offset(PREFER_ITEM_MARGIN);
  50. }
  51. }else if (i == lineArray.count - 1){
  52. make.left.equalTo(frontBtn.mas_right).offset(5);
  53. make.right.mas_greaterThanOrEqualTo(self).offset(- 15);
  54. make.top.equalTo(frontBtn);
  55. }else{
  56. make.left.equalTo(frontBtn.mas_right).offset(5);
  57. make.top.equalTo(frontBtn);
  58. }
  59. make.width.mas_offset(buttonW);
  60. make.height.mas_offset(PREFER_ITEM_HEIGHT);
  61. }];
  62. frontBtn = button;
  63. index++;
  64. if (i == lineArray.count - 1) uperView = button;
  65. }
  66. }
  67. }
  68. - (void)holdContentSizeForLine:(NSUInteger)lineCount{
  69. CGFloat contentH = [self contentHeigthForLineCount:lineCount];
  70. UIView *view = [[UIView alloc]init];
  71. view.userInteractionEnabled = NO;
  72. [self addSubview:view];
  73. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.top.bottom.left.equalTo(self);
  75. make.width.mas_equalTo(1);
  76. make.height.mas_equalTo(contentH);
  77. }];
  78. }
  79. - (CGFloat)contentHeigthForLineCount:(NSUInteger)lineCount{
  80. return lineCount * (PREFER_ITEM_MARGIN + PREFER_ITEM_HEIGHT) + 20;
  81. }
  82. - (NSArray *)groupForLineWithModelArray:(NSArray *)modelArray{
  83. NSMutableArray *groupArray = [NSMutableArray arrayWithCapacity:modelArray.count];
  84. NSMutableArray *singleLineArray = [NSMutableArray arrayWithCapacity:modelArray.count];
  85. CGFloat widthMax = UISCREENWIDTH - 30.0f;
  86. CGFloat addUpWidth = 0.0f;
  87. CGFloat itemMargin = 5.0f;
  88. for (FKPreferItem *item in modelArray) {
  89. CGFloat itemW = [FKPreferButton widthForTitle:item.name];
  90. if (addUpWidth + itemW <= widthMax) {
  91. [singleLineArray addObject:item];
  92. }else{
  93. [groupArray addObject:singleLineArray.copy];
  94. singleLineArray = [NSMutableArray arrayWithCapacity:modelArray.count];
  95. addUpWidth = 0.0f;
  96. [singleLineArray addObject:item];
  97. }
  98. addUpWidth += (itemW + itemMargin);
  99. }
  100. if (singleLineArray.count){
  101. [groupArray addObject:singleLineArray.copy];
  102. }
  103. return groupArray;
  104. }
  105. - (void)clickPreferBtn:(FKPreferButton *)sender{
  106. sender.selected = !sender.selected;
  107. NSInteger index = [self.itemViewArrayM indexOfObject:sender];
  108. if (index != NSNotFound){
  109. if (self.changeSelect) self.changeSelect(index, sender.selected);
  110. }
  111. }
  112. - (void)removeAllSubviews{
  113. for (UIView *view in self.subviews) {
  114. if ([view isKindOfClass:[FKPreferButton class]]){
  115. [view removeFromSuperview];
  116. }
  117. }
  118. }
  119. - (void)resetAllSelectedState{
  120. for (FKPreferButton *button in self.itemViewArrayM) {
  121. [button setSelected:NO];
  122. }
  123. }
  124. - (void)prepareEnoughItemView{
  125. while (self.itemViewArrayM.count < self.modelArray.count) {
  126. FKPreferButton *button = [[FKPreferButton alloc]init];
  127. [button addTarget:self
  128. action:@selector(clickPreferBtn:)
  129. forControlEvents:UIControlEventTouchUpInside];
  130. [self.itemViewArrayM addObject:button];
  131. }
  132. }
  133. - (FKPreferButton *)buttonForIndex:(NSUInteger)index{
  134. if (index < self.itemViewArrayM.count) {
  135. return self.itemViewArrayM[index];
  136. }
  137. return nil;
  138. }
  139. - (void)setModelArray:(NSArray *)modelArray{
  140. _modelArray = modelArray;
  141. [self rebuildSubviews];
  142. }
  143. - (NSMutableArray *)itemViewArrayM{
  144. if (_itemViewArrayM == nil) {
  145. _itemViewArrayM = [[NSMutableArray alloc]init];
  146. }
  147. return _itemViewArrayM;
  148. }
  149. @end