iOS - 将UICollectionViewCell滚动到菜单
2022-12-06 本文已影响0人
温柔vs先生
我的是横向滑动的菜单,点击当前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];
}
}
如果不采用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; i++) {
TYAVLableModel *model = self.titleArr[i];
UIButton *btn = [UIButton buttonWithTitle:model.name titleColor:main_light_text_color font:[UIFont systemFontOfSize:15] target:self action:@selector(titleBtnClick:)];
btn.frame = CGRectMake((jianGe+btnWidth)*i+jianGe, 0, btnWidth, scHeight-1);
btn.tag = i+100;
[topTitleSC addSubview:btn];
if (i == 0) {
_lineLab = [[UILabel alloc] initWithFrame:CGRectMake(jianGe, scHeight-1, btnWidth, 1)];
_lineLab.backgroundColor = main_select_text_color;
[topTitleSC addSubview:_lineLab];
}
selectBtn = btn;
}
topTitleSC.contentSize = CGSizeMake(selectBtn.right+jianGe, topTitleSC.height);
self.scrollView.contentSize = CGSizeMake(self.titleArr.count*KSCREEN_WIDTH, self.scrollView.height);
[self addListVCWithIndex:0];
}
- (void)titleBtnClick:(UIButton *)btn {
if(btn == _currentBtn) return;
[_currentBtn setTitleColor:main_light_text_color forState:UIControlStateNormal];
[btn setTitleColor:main_select_text_color forState:UIControlStateNormal];
_lineLab.center = CGPointMake(btn.center.x, _lineLab.center.y);
[UIView animateWithDuration:0.3 animations:^{
[self.scrollView setContentOffset:CGPointMake((btn.tag-100)*KSCREEN_WIDTH, self.scrollView.contentOffset.y) animated:NO];
}];
[self addListVCWithIndex:btn.tag-100];
// 有数学公式得出的算法
if((KSCREEN_WIDTH - btn.x) < 2*btn.width) {
if((btn.x - (KSCREEN_WIDTH -btn.width)/2.f)<(self.titleSC.contentSize.width-KSCREEN_WIDTH)) {
[UIView animateWithDuration:0.5 animations:^{
self.titleSC.contentOffset = CGPointMake(btn.x - (KSCREEN_WIDTH -btn.width)/2.f, 0);
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.titleSC.contentOffset = CGPointMake(self.titleSC.contentSize.width - KSCREEN_WIDTH, 0);
}];
}
} else {
[UIView animateWithDuration:0.5 animations:^{
self.titleSC.contentOffset = CGPointMake(0, 0);
}];
}
_currentBtn = btn;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger index = (scrollView.contentOffset.x+KSCREEN_WIDTH/2.f)/KSCREEN_WIDTH;
UIButton *btn = (UIButton *)[self.titleSC viewWithTag:index+100];
[self titleBtnClick:btn];
[self addListVCWithIndex:(int)(scrollView.contentOffset.x/KSCREEN_WIDTH)];
}
#pragma mark - addVC
- (void)addListVCWithIndex:(NSInteger)index {
if (index<0||index>=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)];
}
}