2017-09-28 26 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];