集合视图单元动画 tvOS

Collection view cell animated tvOS

我正在尝试为集合视图单元格设置动画,这是我目前的代码

- (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator{

    UICollectionViewCell *nextFocusedCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:context.nextFocusedIndexPath];
    UICollectionViewCell *previousFocusedCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"VideoCell" forIndexPath:context.previouslyFocusedIndexPath];
    if (context.nextFocusedView) {
        [coordinator addCoordinatedAnimations:^{
            [nextFocusedCell setFrame: CGRectMake(3, 14, 300, 300)];
        } completion:^{
            // completion
        }];
    } else if (context.previouslyFocusedView) {
        [coordinator addCoordinatedAnimations:^{
            [previousFocusedCell setFrame: CGRectMake(3, 14, 100, 100)];
        } completion:^{
            // completion
        }];
    }

但是我的代码不起作用。我已经阅读了文档,它说要实现类似的东西 if (self == contextFocusedView)......但是它有一个警告说不兼容的指针 View Controller 到 UIView。有人可以告诉我我的代码有什么问题以及如何修复它吗?谢谢!!

所以我最终弄明白了。我的 collectionView 中有一个 UIImage。 tvOS 的 UIImage 的属性之一是 adjustsImageWhenAncestorFocused。因此,如果您在 viewDidLoad 中将其设置为:

    _imageView.adjustsImageWhenAncestorFocused = Yes;

或者,选中故事板中的 "Adjusts image when focused" 框。这将使图像聚焦。我还没有尝试过标签。或您可以在集合视图单元格中添加的任何其他元素,但我确定有类似的东西。

各位编程愉快! (:

dequeueReusableCellWithReuseIdentifier: 不会return 当前视图而是新视图。 尝试使用 cellForItemAtIndexPath:

但为什么不使用:

if (context.nextFocusedView) {
    [coordinator addCoordinatedAnimations:^{
        context.nextFocusedView.transform = CGAffineTransformMakeScale(1.1, 1.1);
    } completion:nil];
}
if (context.previouslyFocusedView) {
    [coordinator addCoordinatedAnimations:^{
        context.previouslyFocusedView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:nil];
}

Swift hanneski 的回答 2.0 版本:

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    if (context.nextFocusedView != nil) {
        coordinator.addCoordinatedAnimations({() -> Void in
            context.nextFocusedView!.transform = CGAffineTransformMakeScale(1.1, 1.1)
            }, completion: { _ in })
    }
    if (context.previouslyFocusedView != nil) {
        coordinator.addCoordinatedAnimations({() -> Void in
            context.previouslyFocusedView!.transform = CGAffineTransformMakeScale(1.0, 1.0)
            }, completion: { _ in })
    }
}

这将缩放包括所有标签在内的整个单元格...