Ios UICollectionView是否滚动到索引?
伙计们,我需要你们的帮助。。。我通过UICollectionView创建了一个自定义控件段 CustomView.m 我有5个单元格,其中包含带有单元格标题的标签 为了通过UILabel的宽度获得单元格宽度,我以以下方式实现了sizeForItemAtIndexPath方法:Ios UICollectionView是否滚动到索引?,ios,objective-c,xcode,uiscrollview,uicollectionview,Ios,Objective C,Xcode,Uiscrollview,Uicollectionview,伙计们,我需要你们的帮助。。。我通过UICollectionView创建了一个自定义控件段 CustomView.m 我有5个单元格,其中包含带有单元格标题的标签 为了通过UILabel的宽度获得单元格宽度,我以以下方式实现了sizeForItemAtIndexPath方法: -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayou
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [[[self titleSection] objectAtIndex:indexPath.item] sizeWithAttributes:nil];
return CGSizeMake(size.width +35 , self.frame.size.height);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:0];
[collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
UDPassData *data = [UDPassData sharedInstance];
[data.sectionContentCollection scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
if (indexPath == newIndexPath) {
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:newIndexPath];
CGRect cellFrameInSuperview = [collectionView convertRect:attributes.frame toView:collectionView];
data.index = cellFrameInSuperview;
[UIView animateWithDuration:.4 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_horizontalBar.frame = CGRectMake(cellFrameInSuperview.origin.x, _horizontalBar.frame.origin.y, cellFrameInSuperview.size.width , 3);
} completion:nil];
}
我还创建了一个UIView,它具有指示已选择哪个单元格的游标函数。
要设置光标(UIView)的动画,我通过以下方式实现:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [[[self titleSection] objectAtIndex:indexPath.item] sizeWithAttributes:nil];
return CGSizeMake(size.width +35 , self.frame.size.height);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:indexPath.item inSection:0];
[collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
UDPassData *data = [UDPassData sharedInstance];
[data.sectionContentCollection scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
if (indexPath == newIndexPath) {
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:newIndexPath];
CGRect cellFrameInSuperview = [collectionView convertRect:attributes.frame toView:collectionView];
data.index = cellFrameInSuperview;
[UIView animateWithDuration:.4 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_horizontalBar.frame = CGRectMake(cellFrameInSuperview.origin.x, _horizontalBar.frame.origin.y, cellFrameInSuperview.size.width , 3);
} completion:nil];
}
UICollectionViewController.m 现在,在另一个ViewController中,我有另一个UICollectionView,它具有水平滚动功能,与段控件共享索引路径,因此我甚至可以使用自定义段控件控制collectionView的滚动。。在这里,一切正常,我设法得到了一切,但我只是遇到了一个问题 当uicollection视图滚动时,段控制滑块不移动。。。简而言之,如果collectionView位于单元格2中,则自定义段控件光标应移动到我创建的段控件中的第二个单元格。。我希望能够解释
编辑:我想得到的是我在Youtube应用程序中看到的。顶部菜单 我正在使用2 collectionView进行此操作 第一个collectionView是菜单(内置于UIView自定义类中)
第二个collectionView在页面之间浏览,位于外部UIViewController中,请确保重新加载collectionView数据源以刷新它
[self.collection1 performBatchUpdates:^{
[collectionView reloadData]
} completion:nil];
我很确定你只需要注意零的速度?我不明白你的意思。。。我不知道如何通过scrollViewDidScroll功能控制段控制滑块,该功能是另一个Classic的一部分。我不确定我是否理解了您的问题,但实现了
UICollectionView
委托的didScroll:
(它继承到UIScrollView
,或您认为合适的任何其他UIScrollViewDelegate
方法),当它继承时,您可以滚动或更改另一个。@Larme我可以和您简短地聊一聊吗?