kAiN
kAiN

Reputation: 2773

UICollectionView did scroll to index?

Guys I need your help ... I created a custom control segment through an UICollectionView ..

CustomView.m

I have 5 cells that contain a label with the title of the cell To get the cell width by the width of the UILabel I implemented the sizeForItemAtIndexPath method in this way:

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

}

I also created a UIView that has the cursor function that indicates which cell has been selected. To animate the cursor (UIView) I implemented this way:

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

now in another ViewController i have another UICollectionView with horizontal scrolling that shares the index path with the segment control so that I can control the scrolling of the collectionView even with the custom segment control .. To here everything works and I managed to get everything but I just have a problem ..

The segment control slider does not move when uicollection view scrolls ... In short, if the collectionView is in cell two, the custom segment control cursor should move to the second cell in the segment control I created .. I hope to to be able to explain

enter image description here


EDIT : What I'm trying to get is this I've seen from the Youtube application. The top menu ...

enter image description here

I'm doing this with 2 collectionView.

the first collectionView is the menu (built into a UIView custom class)

the second collectionView is browsing between pages and is located in an external UIViewController

Upvotes: 0

Views: 668

Answers (1)

casillas
casillas

Reputation: 16793

Make sure to reload collectionView Data Source in order to fresh it.

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

Upvotes: 1

Related Questions