0

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

kAiN
  • 2,559
  • 1
  • 26
  • 54
  • I'm pretty sure you just have to watch for velocity of zero? – Fattie Sep 28 '17 at 18:57
  • I do not understand what you mean ... I do not know how to control the segment control slider through the scrollViewDidScroll function that is part of another class – kAiN Sep 28 '17 at 19:00
  • I'm not sure that I did understood your issue, but implement `didScroll:` of `UICollectionView` delegate (which inherits to `UIScrollView`, or any other `UIScrollViewDelegate` method that you'd found appropriate), and when it did, you can scroll or change the other one. – Larme Sep 29 '17 at 08:05
  • @Larme can I chat briefly with you? – kAiN Sep 29 '17 at 08:06
  • https://stackoverflow.com/questions/30398339/ios-how-sync-two-uiscrollview https://stackoverflow.com/questions/9207474/double-uiscrollview-synchronization-different-height https://stackoverflow.com/questions/17303772/manually-scrolling-two-uicollectionviews-showing-the-same-content ? – Larme Sep 29 '17 at 08:09
  • @Larme Definitely I explained to you badly. What I want is to get the same result as I select cells, using the scrollview of a collectionView included in another view controller. In viewController 2 the function scrollViewDidScroll what value should I pass based on what I did in didSelectItemAtIndexPath in viewController 1? – kAiN Sep 29 '17 at 08:13
  • You could create your own delegate protocol. When there is a change you can "contact" the other one to tell it to do something (scroll, or whatever). – Larme Sep 29 '17 at 08:52
  • @Larme for this I had thought in fact ... could you give me a quick example using my didSelectItem in the code I posted here in the application to understand well? – kAiN Sep 29 '17 at 08:54

1 Answers1

1

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

[self.collection1 performBatchUpdates:^{
   [collectionView reloadData]
 } completion:nil];
casillas
  • 16,351
  • 19
  • 115
  • 215