在焦点更改期间动画化子图层

时间:2016-08-22 01:58:57

标签: objective-c animation calayer tvos

Xcode 8 beta 6,tvOS Beta 6

我有一个tvOS应用程序,我想在控件的背景设置或失去焦点时为其设置动画。我已将控件设置为自定义'关注并实施didUpdateFocusInContext:withAnimationCoordinator:控件。这是代码:

-(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
  withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {

    // Create the layer if we don't have it.
    if (!self->_focusLayer) {
    // ... Create a new CALayer and store it in _focusLayer
    }

    // Animate in or out.
    if (context.nextFocusedView == self) {

        if (! self->_focusLayer.superlayer) {
            STLog(self, @"Adding focus");
            self->_focusLayer.opacity = 0.0f;
            [self.layer addSublayer:self->_focusLayer];
            [coordinator addCoordinatedAnimations:^{
                self->_focusLayer.opacity = 1.0f;
            }
                                       completion:NULL];
        }

    } else {

        if (self->_focusLayer.superlayer) {
            STLog(self, @"Removing focus");
            [coordinator addCoordinatedAnimations:^{
                self->_focusLayer.opacity = 0.0f;
            }
                                       completion:^{
                                           [self->_focusLayer removeFromSuperlayer];
                                       }];
        }
    }
}

除了子图层不透明度的动画外,一切都有效。  我在网上搜索过,我发现的所有例子都告诉我这应该有效。我也尝试过使用CABasicAnimation而没有运气。

任何人都知道为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

焦点协调器本身不是动画块。它只是协调各种动画同时发生。由于不透明度变化本身不是动画,因此您需要在UIView动画块中进行不透明度或Alpha变化以使其成为动画,并将其添加到协调器中。

试试这个:

-(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context
      withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {

    // Create the layer if we don't have it.
    if (!self->_focusLayer) {
        // ... Create a new CALayer and store it in _focusLayer
    }

    // Animate in or out.
    if (context.nextFocusedView == self) {

        if (! self->_focusLayer.superlayer) {
            STLog(self, @"Adding focus");
            self->_focusLayer.opacity = 0.0f;
            [self.layer addSublayer:self->_focusLayer];
            [coordinator addCoordinatedAnimations:^{

                [UIView animateWithDuration: 0.4 animations:^{
                    self->_focusLayer.opacity = 1.0f;
                } completion:NULL];

            } completion:NULL];
        }

    } else {

        if (self->_focusLayer.superlayer) {
            STLog(self, @"Removing focus");
            [coordinator addCoordinatedAnimations:^{

                [UIView animateWithDuration:0.4 animations:^{
                    self->_focusLayer.opacity = 0.0f;
                } completion:NULL];

            } completion:^{
                [self->_focusLayer removeFromSuperlayer];
            }];
        }
    }
}

请注意,上面的代码在此输入,未在项目中测试。

另请注意,动画协调器会忽略动画持续时间,并使用默认焦点动画持续时间,除非您使用以下方法设置动画选项以覆盖继承的持续时间:

options:UIViewAnimationOptionOverrideInheritedDuration

有关管理焦点动画协调员的更多信息:

https://developer.apple.com/reference/uikit/uifocusanimationcoordinator