2017-09-28 27 views
0

家伙,我需要你们的帮助......我创建通过UICollectionView自定义控件段..UICollectionView确实滚动到索引?

CustomView.m

我有一个包含一个标签与细胞 的标题为5个细胞由的UILabel的宽度I以这种方式实现的sizeForItemAtIndexPath方法获得单元宽度:

-(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); 

} 

我还创建具有光标功能,其指示哪个单元已经被选择一个UIView。现在

-(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

在另一个视图控制器我有同方股份的索引路径与水平滚动另一UICollectionView: 要动画光标(UIView的)我这种方式来实现段控制,以便我可以控制滚动的collectionView即使与自定义段控制..到这里一切正常,我设法得到的一切,但我只是有一个问题..

当uicollection视图滚动时,段控件滑块不会移动...总之,如果collectionView在单元格2中,自定义段控件游标应该移动到我创建的段控件中的第二个单元格中。能够解释

enter image description here


编辑:我想要得到的是这个我已经从Youtube上看到的应用。顶部菜单...

enter image description here

我与2的CollectionView这样做。

第一的CollectionView是菜单(内置于一个UIView自定义类)

第二的CollectionView的页面之间浏览和位于外部的UIViewController

+0

我很确定你只需要注意零速度? – Fattie

+0

我不明白你的意思......我不知道如何通过scrollViewDidScroll函数控制段控制滑块,该函数是另一个类的一部分 – kAiN

+0

我不确定我是否理解了您的问题,但实现了'didScroll: 'UICollectionView'委托(它继承到'UIScrollView',或者你认为合适的任何其他'UIScrollViewDelegate'方法),当它做到时,你可以滚动或改变另一个。 – Larme

回答

1

确保reload的CollectionView数据源,以新鲜它。

[self.collection1 performBatchUpdates:^{ 
    [collectionView reloadData] 
} completion:nil];