Objective c 在焦点更改期间设置子层动画

Objective c 在焦点更改期间设置子层动画,objective-c,animation,calayer,tvos,Objective C,Animation,Calayer,Tvos,Xcode 8 beta 6,tvOS beta 6 我有一个tvOS应用程序,当控件获得或失去焦点时,我想在其中设置控件背景动画。我已将控件设置为具有“自定义”焦点,并在控件上实现了didUpdateFocusInText:withAnimationCoordinator:。代码如下: -(void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimati

Xcode 8 beta 6,tvOS beta 6

我有一个tvOS应用程序,当控件获得或失去焦点时,我想在其中设置控件背景动画。我已将控件设置为具有“自定义”焦点,并在控件上实现了
didUpdateFocusInText: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
,但运气不好


有人知道为什么这不起作用吗?

焦点协调器本身不是动画块。它只是协调各种动画同时发生。由于不透明度更改本身不是动画,因此需要在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
有关管理焦点动画协调器的详细信息,请参见:


谢谢。我会调查的。