// // FKPreferScrollView.m // FirstLink // // Created by jack on 16/5/11. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKPreferScrollView.h" #import "FKPreferButton.h" #import "FKPreferItem.h" #define PREFER_ITEM_MARGIN 29 #define PREFER_ITEM_HEIGHT 35 @interface FKPreferScrollView () @property (nonatomic, strong) NSMutableArray *itemViewArrayM; @end @implementation FKPreferScrollView - (instancetype)initWithIndexModelArray:(NSArray *)modelArray{ if (self = [super init]) { _modelArray = modelArray; [self rebuildSubviews]; } return self; } - (void)rebuildSubviews{ if (self.modelArray.count == 0) return; [self removeAllSubviews]; [self resetAllSelectedState]; [self prepareEnoughItemView]; NSArray *lineGroupArray = [self groupForLineWithModelArray:self.modelArray]; [self holdContentSizeForLine:lineGroupArray.count]; NSUInteger index = 0; UIView *uperView = nil; for (NSArray *lineArray in lineGroupArray) { FKPreferButton *frontBtn = nil; for (NSInteger i = 0; i < lineArray.count; i++) { FKPreferButton *button = self.itemViewArrayM[index]; if (!button) continue; [self addSubview:button]; FKPreferItem *item = lineArray[i]; CGFloat buttonW = [FKPreferButton widthForTitle:item.name]; button.title = item.name; button.selected = item.selected; [button mas_makeConstraints:^(MASConstraintMaker *make) { if (i == 0){ make.left.equalTo(self).offset(15); if (uperView){ make.top.equalTo(uperView.mas_bottom).offset(PREFER_ITEM_MARGIN); }else{ make.top.equalTo(self).offset(PREFER_ITEM_MARGIN); } }else if (i == lineArray.count - 1){ make.left.equalTo(frontBtn.mas_right).offset(5); make.right.mas_greaterThanOrEqualTo(self).offset(- 15); make.top.equalTo(frontBtn); }else{ make.left.equalTo(frontBtn.mas_right).offset(5); make.top.equalTo(frontBtn); } make.width.mas_offset(buttonW); make.height.mas_offset(PREFER_ITEM_HEIGHT); }]; frontBtn = button; index++; if (i == lineArray.count - 1) uperView = button; } } } - (void)holdContentSizeForLine:(NSUInteger)lineCount{ CGFloat contentH = [self contentHeigthForLineCount:lineCount]; UIView *view = [[UIView alloc]init]; view.userInteractionEnabled = NO; [self addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.equalTo(self); make.width.mas_equalTo(1); make.height.mas_equalTo(contentH); }]; } - (CGFloat)contentHeigthForLineCount:(NSUInteger)lineCount{ return lineCount * (PREFER_ITEM_MARGIN + PREFER_ITEM_HEIGHT) + 20; } - (NSArray *)groupForLineWithModelArray:(NSArray *)modelArray{ NSMutableArray *groupArray = [NSMutableArray arrayWithCapacity:modelArray.count]; NSMutableArray *singleLineArray = [NSMutableArray arrayWithCapacity:modelArray.count]; CGFloat widthMax = UISCREENWIDTH - 30.0f; CGFloat addUpWidth = 0.0f; CGFloat itemMargin = 5.0f; for (FKPreferItem *item in modelArray) { CGFloat itemW = [FKPreferButton widthForTitle:item.name]; if (addUpWidth + itemW <= widthMax) { [singleLineArray addObject:item]; }else{ [groupArray addObject:singleLineArray.copy]; singleLineArray = [NSMutableArray arrayWithCapacity:modelArray.count]; addUpWidth = 0.0f; [singleLineArray addObject:item]; } addUpWidth += (itemW + itemMargin); } if (singleLineArray.count){ [groupArray addObject:singleLineArray.copy]; } return groupArray; } - (void)clickPreferBtn:(FKPreferButton *)sender{ sender.selected = !sender.selected; NSInteger index = [self.itemViewArrayM indexOfObject:sender]; if (index != NSNotFound){ if (self.changeSelect) self.changeSelect(index, sender.selected); } } - (void)removeAllSubviews{ for (UIView *view in self.subviews) { if ([view isKindOfClass:[FKPreferButton class]]){ [view removeFromSuperview]; } } } - (void)resetAllSelectedState{ for (FKPreferButton *button in self.itemViewArrayM) { [button setSelected:NO]; } } - (void)prepareEnoughItemView{ while (self.itemViewArrayM.count < self.modelArray.count) { FKPreferButton *button = [[FKPreferButton alloc]init]; [button addTarget:self action:@selector(clickPreferBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.itemViewArrayM addObject:button]; } } - (FKPreferButton *)buttonForIndex:(NSUInteger)index{ if (index < self.itemViewArrayM.count) { return self.itemViewArrayM[index]; } return nil; } - (void)setModelArray:(NSArray *)modelArray{ _modelArray = modelArray; [self rebuildSubviews]; } - (NSMutableArray *)itemViewArrayM{ if (_itemViewArrayM == nil) { _itemViewArrayM = [[NSMutableArray alloc]init]; } return _itemViewArrayM; } @end