// // JZOptionButtonView.m // JIZHANG // // Created by kuxuan on 2017/10/27. // Copyright © 2017年 kuxuan. All rights reserved. // #import "JZOptionButtonView.h" #import "JZOptionCollectionViewCell.h" #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) @interface JZOptionButtonView() @property (nonatomic,strong)NSIndexPath *indexPath; @end @implementation JZOptionButtonView { UICollectionView *_collectionView; UICollectionViewFlowLayout *_collectionViewLayout; } -(id)initWithFrame:(CGRect)frame { if (self==[super initWithFrame:frame]) { [self setupView]; } return self; } -(void)setupView { _collectionViewLayout= [[UICollectionViewFlowLayout alloc] init]; _collectionViewLayout.itemSize=CGSizeMake(100, self.frame.size.height); _collectionViewLayout.minimumLineSpacing=0; _collectionViewLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:_collectionViewLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.showsVerticalScrollIndicator = NO; [_collectionView registerClass:[JZOptionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; [self addSubview:_collectionView]; } #pragma mark - - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.titleArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { JZOptionCollectionViewCell *cell = (JZOptionCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; [cell setTitleColor:self.selectedColor forState:KXOptionSelectStateSelected]; [cell setTitleColor:self.nomalColor forState:KXOptionSelectStateNomal]; if (indexPath.item==self.Selectindex) { cell.selection=YES; }else{ cell.selection=NO; } cell.titleLabel.text=self.titleArray[indexPath.row]; cell.titleLabel.font=self.titleFont; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { self.Selectindex = indexPath.item; [self selectedBtnAtIndex:indexPath.item]; self.operation(indexPath.item); } - (void)selectedBtnAtIndex:(NSInteger)index { NSArray *visibleArray = _collectionView.visibleCells; for (JZOptionCollectionViewCell *cell in visibleArray) { cell.selection = NO; } JZOptionCollectionViewCell *Cell=(JZOptionCollectionViewCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; Cell.selection=YES; self.indexPath=[NSIndexPath indexPathForItem:index inSection:0]; CGPoint center=Cell.center; if (Cell == nil) { [UIView animateWithDuration:0.4 animations:^{ if (self.titleArray.count*100 > ScreenWidth) { if (self.titleArray.count - 1 - self.Selectindex >= 2 && self.Selectindex >= 2) { _collectionView.contentOffset = CGPointMake(self.titleArray.count*100 - ScreenWidth/2 - (self.titleArray.count - 1 - self.Selectindex)*100 - 50, 0); } else if (self.Selectindex <= 2) { _collectionView.contentOffset = CGPointMake(self.Selectindex *100, 0); } else { _collectionView.contentOffset = CGPointMake(self.titleArray.count*100 - ScreenWidth, 0); } } }]; return; } if (center.x > self.frame.size.width/2) { if (_collectionView.contentSize.width self.frame.size.width) { if (center.x - self.frame.size.width/2>_collectionView.contentSize.width-self.frame.size.width) { [UIView animateWithDuration:0.4 animations:^{ _collectionView.contentOffset = CGPointMake(_collectionView.contentSize.width-self.frame.size.width, 0); }]; }else{ [UIView animateWithDuration:0.4 animations:^{ _collectionView.contentOffset = CGPointMake(center.x - self.frame.size.width/2, 0); }]; } }else { [UIView animateWithDuration:0.4 animations:^{ _collectionView.contentOffset = CGPointMake( ScreenWidth -_collectionView.contentSize.width, 0); }]; } return; }]; }else{ [UIView animateWithDuration:0.4 animations:^{ _collectionView.contentOffset = CGPointMake(0, 0); }]; } } - (void)setSelectindex:(NSInteger)Selectindex{ _Selectindex = Selectindex; [self selectedBtnAtIndex:Selectindex]; } -(void)setItemSize:(CGSize)itemSize { _itemSize=itemSize; _collectionViewLayout.itemSize=itemSize; [_collectionView reloadData]; } - (void)setTitleArray:(NSArray *)titleArray { _titleArray = titleArray; _indexPath=[NSIndexPath indexPathForItem:titleArray.count-1 inSection:0]; [_collectionView reloadData]; } -(void)setMaxVisible:(NSInteger)maxVisible { _maxVisible=maxVisible; if (maxVisible*_collectionViewLayout.itemSize.width