在焦点更改期间为子层设置动画
Animating a sub layer during focus change
Xcode 8 测试版 6,tvOS 测试版 6
我有一个 tvOS 应用程序,我想在它获得或失去焦点时为控件的背景制作动画。我已将控件设置为 'Custom' 焦点并在控件上实现了 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
没有运气。
有人知道为什么这不起作用吗?
焦点协调器本身不是动画块。它只是协调各种动画同时发生。由于不透明度更改本身不是动画,因此您需要在 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
Xcode 8 测试版 6,tvOS 测试版 6
我有一个 tvOS 应用程序,我想在它获得或失去焦点时为控件的背景制作动画。我已将控件设置为 'Custom' 焦点并在控件上实现了 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
没有运气。
有人知道为什么这不起作用吗?
焦点协调器本身不是动画块。它只是协调各种动画同时发生。由于不透明度更改本身不是动画,因此您需要在 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