Disable focus scrolling UICollectionView

Is there a way to turn off UICollectionView auto-scrolling when the cell is focused? I want to adjust the offset of the contents of the cell manually when it comes into focus.

I do not want 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 get an animation that is not smooth, there is a “kick” at the end.

+5
source share
3 answers

Just stumbled upon this thing. To disable automatic scrolling of the UICollectionView when the focus changes, simply disable scrolling in the "View Scrolling" properties to represent the collection in the interface builder:

enter image description here

+1
source

I don’t know what you mean by “automatic scrolling”, but to change the cell of the collection view you can use the didUpdateFocusInContext file 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 do not have a custom cell, use the didUpdateFocusInContext file 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) } 
-1
source

Because UICollectionView subclass of UIScrollView , your collection view delegate can implement scroll delegation methods. You probably want:

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

targetContentOffset will be the scroll offset that UIKit will do by default, but if you change the value then it will scroll UIKit instead.

If you really don't want UIKit to do any kind of automatic scrolling for you at all, you can always just turn off scrolling completely by setting scrollEnabled to NO .

-1
source

Source: https://habr.com/ru/post/1235202/


All Articles