【问题标题】:Disable focus triggered scrolling of UICollectionView禁用 UICollectionView 的焦点触发滚动
【发布时间】:2015-11-04 14:01:52
【问题描述】:

有没有办法在单元格获得焦点时禁用UICollectionView 的自动滚动?我想在单元格聚焦时手动调整单元格的内容偏移量。

我不想更新内容偏移:

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

这可行,但由于焦点引擎也会移动我的单元格(滚动它),我最终会得到不流畅的动画,在它的末尾有一个“踢”。

【问题讨论】:

    标签: uicollectionview tvos apple-tv


    【解决方案1】:

    刚刚遇到了这个确切的事情。要在焦点更改时禁用 UICollectionView 的自动滚动,只需在界面构建器中的集合视图的“滚动视图”属性中禁用滚动:

    【讨论】:

      【解决方案2】:

      我不知道“自动滚动”是什么意思,但要更改集合视图单元格,您可以在自定义单元格类中使用 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)
      }
      

      或者,如果您没有自定义单元格,请使用 UICollectionViewDelegate 中的 didUpdateFocusInContext

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

      【讨论】:

      • 对不起,我想你误解了我的问题。我发布了更多代码。我需要的是有一个集合视图,当焦点从一个单元格遍历到另一个单元格时,它不会自动更新内容偏移量。我想自己手动进行内容偏移操作。
      【解决方案3】:

      由于UICollectionViewUIScrollView 的子类,您的集合视图委托可以实现滚动视图委托方法。您可能想要的是:

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

      targetContentOffset 将是 UIKit 默认滚动到的滚动偏移量,但如果您更改值,UIKit 将改为滚动到那里。

      如果你真的不希望 UIKit 为你做任何自动滚动,你总是可以通过将 scrollEnabled 设置为 NO 来完全禁用滚动。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2021-11-03
        • 2011-09-09
        • 1970-01-01
        相关资源
        最近更新 更多