2015-11-04 119 views
5

有没有一种方法可以禁用细胞聚焦时自动滚动UICollectionView?我想在调整焦点时手动调整单元格的内容偏移量。禁用聚焦触发滚动的UICollectionView

我wan't更新内容偏移:

- (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]; 
} 

这工作,但由于焦点引擎也移动我的手机(滚动它)我最终的动画是不光滑,有“踢”在它的结尾。

回答

-1

我不知道你的意思与“自动滚动”的东西,但改变你可以使用didUpdateFocusInContext在您的自定义单元格类集合视图细胞。

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) 
} 

或者,如果你没有一个自定义单元格,使用didUpdateFocusInContext从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) 
} 
+0

对不起,我想你是误会我的问题。我发布了更多的代码。我需要的是有一个集合视图,当焦点从一个单元格移动到另一个单元格时,不会自动更新内容偏移量。我想自己手动执行内容偏移操作。 – RaffAl

-1

由于UICollectionView子类UIScrollView,您的集合视图委托可以实现滚动视图的委托方法。你可能想的一个是:

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

targetContentOffset将滚动偏移的UIKit将默认滚动,但如果你改变了值,那么会的UIKit反而有滚动。

如果你确实不希望UIKit为你做任何自动滚动,你总是可以通过设置scrollEnabledNO来完全禁用滚动。

1

刚才碰到了这个确切的东西。要禁用UICollectionView的自动滚动当焦点的变化,只是禁用在接口生成器中的集合视图的“滚动查看”属性滚动:

enter image description here