iOS - 将UICollectionViewCell滚动到菜单中心

我的是横向滑动的菜单,点击当前cell时,让其滚动到中间位置,注意要判断最左边和最右边的cell,点击时不能发生偏移,以下是我点击时的代码:

    UICollectionViewLayoutAttributes * att = [self.collectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    CGFloat width = self.collectionView.width;
    if(self.collectionView.contentSize.width > width) {
        if(att.frame.origin.x > width/2.f) {
    //        NSLog(@"%f ---- %f", width/2.f, self.collectionView.contentSize.width - att.frame.origin.x);
            if(self.collectionView.contentSize.width - att.frame.origin.x - att.bounds.size.width/2.f > width/2.f) {
                CGPoint point = CGPointMake(att.frame.origin.x - width/2.f + att.bounds.size.width/2.f , 0);
                [self.collectionView setContentOffset:point animated:YES];
            }else {
                [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentSize.width - width, 0) animated:YES];
            }
        }else {
            [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    }

列表滚动到特定的cell

/// 下面代理是在UICollectionView 刷表完成后执行的
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  {
      NSInteger index = [self.dataArr indexOfObject:self.selectModel];
      /// 保证collectionView全部加载完毕
        if([indexPath row] == ((NSIndexPath*)[[collectionView indexPathsForVisibleItems] lastObject]).row){
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
            
        }
   }

如果不采用UICollectionView制作菜单,用UIScrollView和UIButton制作,代码如下:

- (void)setUI {

    UIScrollView *topTitleSC = [[UIScrollView alloc] init];
    topTitleSC.showsHorizontalScrollIndicator = NO;
    topTitleSC.showsVerticalScrollIndicator = NO;
    [self.view addSubview:topTitleSC];
    self.titleSC = topTitleSC;
    [topTitleSC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
        make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        make.height.offset(scHeight);
    }];
    
    
    
    UIButton *selectBtn = nil;
    for (int i =0; i=self.titleArr.count) {
        return;
    }
    //根据页数添加相对应的视图 并存入数组
    
    if (![_listVCQueue objectForKey:@(index)]) {
        TYAVHomeViewController *contentVC = [[TYAVHomeViewController alloc] init];
        [self addChildViewController:contentVC];
        TYAVLableModel *model =  self.titleArr[index];
        contentVC.vClass = model.name;

        contentVC.view.frame = CGRectMake(KSCREEN_WIDTH*index, 0, self.scrollView.width, self.scrollView.height);
        [self.scrollView addSubview:contentVC.view];
        
        [_listVCQueue setObject:contentVC forKey:@(index)];
    }
}

https://www.cnblogs.com/sunfuyou/p/7460825.html

你可能感兴趣的:(iOS - 将UICollectionViewCell滚动到菜单中心)