CollectionView按鈕聯動

CollectioView滾動到指定section的方法
CollectionView按鈕聯動

實現1:點擊按鈕,CollectionView滾動到指定位置
實現2:滑動CollectionView,根據屏幕中心判斷某按鈕高亮

這里貼下最近剛寫的需求代碼,希望能幫到有需要的人,歡迎留言


IMG_0381.PNG

按鈕九宮格代碼創建

    CGFloat sudokuViewY = self.topCateGoryCollectionView.mj_y + self.topCateGoryCollectionView.mj_h+10;
    CGFloat sudokuViewW = YPWidthOfScreen-20;
    UIView *sudokuView = [[UIView alloc]initWithFrame:CGRectMake(10, sudokuViewY, sudokuViewW, 100)];
    sudokuView.backgroundColor = [UIColor blueColor];
    [self.topView addSubview:sudokuView];
    
    self.btnArray = [NSMutableArray array];
    for (int i=0; i<self.listTitleArray.count; i++) {
        CGFloat btnMargin = 5;
        CGFloat btnW = (sudokuViewW - btnMargin*2) / 3;
        CGFloat btnH = 30;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = BtnTag+i;
        [button.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
        HeadTagModel *model = self.listTitleArray[i];
        [button setTitle:model.Name forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage WY_imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage WY_imageWithColor:[UIColor orangeColor]] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage WY_imageWithColor:[UIColor orangeColor]] forState:UIControlStateHighlighted];
        button.layer.cornerRadius = btnH*0.5;
        button.layer.masksToBounds = YES;
        [button.layer setBorderColor:[UIColor orangeColor].CGColor];
        [button.layer setBorderWidth:1.0];
        [button addTarget:self action:@selector(clickSudokuViewBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i >= 0 && i < 3) {
            [button setFrame:CGRectMake(0+i*btnMargin+i*btnW, 0, btnW, btnH)];
        }else if(i >= 3 && i < 6){
            NSInteger num = i-3;
            [button setFrame:CGRectMake(0+num*btnMargin+num*btnW, btnH+btnMargin, btnW, btnH)];
        }else{
            NSInteger num = i-6;
            [button setFrame:CGRectMake(0+num*btnMargin+num*btnW, 2*btnH+btnMargin*2, btnW, btnH)];
        }
        if (i == 0) {
            self.selectedBtn = button;
            [self clickSudokuViewBtn:button];
        }
        [sudokuView addSubview:button];
        [self.btnArray addObject:button];
    }

按鈕點擊事件

-(void)clickSudokuViewBtn:(UIButton *)btn{
    //點擊滾動到指定位置
//保證collectionView全部加載完畢,我這里通過一個bool的標志位來標示
    if (self.Is_ListLoad) {
        NSInteger index = btn.tag - BtnTag;
        [self.listCollectionView layoutIfNeeded ];
        NSIndexPath* cellIndexPath = [NSIndexPath indexPathForItem:0 inSection:index];
        UICollectionViewLayoutAttributes* attr = [self.listCollectionView.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:cellIndexPath];
        UIEdgeInsets insets = self.listCollectionView.scrollIndicatorInsets;
        CGRect rect = attr.frame;
        rect.size = self.listCollectionView.frame.size;
        rect.size.height -= insets.top + insets.bottom;
        CGFloat offset = (rect.origin.y + rect.size.height) - self.listCollectionView.contentSize.height;
        if ( offset > 0.0 ) rect = CGRectOffset(rect, 0, -offset);
        [self.listCollectionView scrollRectToVisible:rect animated:YES];
    }
    //按鈕顏色
    if (btn!= self.selectedBtn) {
        self.selectedBtn.selected = NO;
        btn.selected = YES;
        self.selectedBtn = btn;
    }else{
        self.selectedBtn.selected = YES;
    }
}

//加載完畢
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.listCollectionView) {
        if([indexPath row] == ((NSIndexPath*)[[self.listCollectionView indexPathsForVisibleItems] lastObject]).row){
            self.Is_ListLoad = YES;
        }
    }
}

滾動CollectionView,按鈕高亮,用2個停止監聽代理,容錯高

#pragma mark - scrollView 停止滾動監測
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 停止類型1
    if (scrollView == self.listCollectionView) {
        BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
        if (scrollToScrollStop) {
            [self scrollViewDidEndScroll];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView == self.listCollectionView) {
        if (!decelerate) {
            // 停止類型2
            BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
            if (dragToDragStop) {
                [self scrollViewDidEndScroll];
            }
        }
    }
}

- (void)scrollViewDidEndScroll {
    UICollectionViewLayoutAttributes *attribute = [self.listCollectionView.collectionViewLayout layoutAttributesForElementsInRect:(CGRect){.origin = self.listCollectionView.contentOffset, .size = self.listCollectionView.bounds.size}][0];
    UIEdgeInsets insets = self.listCollectionView.scrollIndicatorInsets;
    CGRect rect = attribute.frame;
    rect.size = self.listCollectionView.frame.size;
    rect.size.height -= insets.top + insets.bottom;
    CGFloat offset = (rect.origin.y + rect.size.height) - self.listCollectionView.contentSize.height;
    if ( offset > 0.0 ) rect = CGRectOffset(rect, 0, -offset);
    attribute.frame = rect;
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(attribute.frame), CGRectGetMidY(attribute.frame));
    NSIndexPath *indexPath = [self.listCollectionView indexPathForItemAtPoint:visiblePoint];
    UIButton *button = self.btnArray[indexPath.section];
    [self testBtn:button];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 每天學一招,輕松做生意 攻心營銷案例,幫你打造賺錢的頭腦 所有的生意都離不開以下三點,第一大量開發新客戶,第二讓新...
    誰家的傻姑涼閱讀 398評論 0 1
  • 2018年12月19日 星期三 今天在睡前認真學習了曹坤老師的《父母視野決定孩子的世界》,了解了家庭教育多么的迫切...
    Annie_4780閱讀 198評論 0 1
  • 春上四月,是光陰對于人間的一種款待。 當我們目處陌上青煙,唯有素心獨對這一場風月,才是最大的不辜負
    冷月_影閱讀 277評論 0 7
  • 哈哈……你的主意真不錯!”宿舍里四個先后從學校門走出來的女孩子百無聊賴地想著刺激的事情來打發時間。海艷說:“我最大...
    平凡的心路閱讀 144評論 0 0
  • 作于2014.3 高鐵上 車窗外 風光惹人愛 白云飄 陽光照 天氣真不賴 心情好 興致高 出差速度快 一季度 三月...
    FriendlyWang閱讀 211評論 0 1