Disable focus triggered scrolling of UICollectionView
Asked Answered
T

3

7

Is there a way to disable automatic scrolling of the UICollectionView when cell gets focused? I want to adjust the content offset of the cell manually when it gets into focus.

I wan't to update the content offset in:

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
       withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    [coordinator addCoordinatedAnimations:^
     {
         [UIView animateWithDuration:[UIView inheritedAnimationDuration]
                          animations:^
          {
              // Move next focused cell.
              if ([context.nextFocusedView isKindOfClass:[YBZEventCollectionViewCell class]])
              {
                  UICollectionViewCell *cell = (UICollectionViewCell *)context.nextFocusedView;

                  CGPoint offset = CGPointMake(CGRectGetMinX(cell.frame), 0.0f);

                  [_collectionView setContentOffset:offset];
              }
          }];

     } completion:nil];
}

This works but since the focus engine also moves my cell (scrolls it) I end up with animation which is not smooth, there is a "kick" at the end of it.

Typecast answered 4/11, 2015 at 14:1 Comment(0)
C
3

Just ran into this exact thing. To disable automatic scrolling of a UICollectionView when focus changes, just disable scrolling in the 'Scroll View' properties for the collection view in interface builder:

enter image description here

Cuckoo answered 24/10, 2017 at 12:2 Comment(1)
collectionView.isScrollEnabled in codeRives
R
-1

I don't know what you mean with 'automatic scrolling', but to alter a collection view cell you could use the didUpdateFocusInContext in your custom cell class.

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    coordinator.addCoordinatedAnimations({ [unowned self] in
        if self.focused {
            self.titleTopConstraint.constant = 27

            self.titleLabel.textColor = UIColor.whiteColor()
        } else {
            self.titleTopConstraint.constant = 5

            self.titleLabel.textColor = UIColor(red: 100 / 255, green: 100 / 255, blue: 100 / 255, alpha: 1)
        }
        self.layoutIfNeeded()
    }, completion: nil)
}

Or if you don't have a custom cell, use the didUpdateFocusInContext from the UICollectionViewDelegate.

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    coordinator.addCoordinatedAnimations({ () -> Void in
        if let indexPath = context.nextFocusedIndexPath, let cell = collectionView.cellForItemAtIndexPath(indexPath) {
            // the cell that is going to be focused
        }
        if let indexPath = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItemAtIndexPath(indexPath) {
            // the cell that is going to be unfocused
        }
    }, completion: nil)
}
Ringo answered 4/11, 2015 at 14:21 Comment(1)
Sorry, I think you misunderstood my question. I posted some more code. What I need is to have a collection view that does not automatically update content offset when focus traverses from one cell to another. I want to do the content offset manipulation manually on my own.Paulin
S
-1

Since UICollectionView subclasses UIScrollView, your collection view delegate can implement scroll view delegate methods. The one you probably want is:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                 withVelocity:(CGPoint)velocity
          targetContentOffset:(inout CGPoint *)targetContentOffset

The targetContentOffset will be the scroll offset that UIKit will scroll to by default, but if you change the value then UIKit will scroll there instead.

If you really truly don't want UIKit to do any automatic scrolling for your whatsoever, you can always just disable scrolling entirely by setting scrollEnabled to NO.

Samirasamisen answered 4/11, 2015 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.