我有一个带有按钮的UIView
,需要在将 View 添加到self.view
时将焦点设置在按钮上,但是我不知道为什么在添加 View 时按钮不聚焦!
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator: (UIFocusAnimationCoordinator *)coordinator {
if (context.nextFocusedView == backButton) {
[UIView animateWithDuration:1 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.60 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
context.nextFocusedView.transform = CGAffineTransformMakeScale(1.5, 1.5);
context.nextFocusedView.layer.shadowOffset = CGSizeMake(0, 10);
context.nextFocusedView.layer.shadowOpacity = 1;
context.nextFocusedView.layer.shadowRadius = 15;
context.nextFocusedView.layer.shadowColor = [UIColor redColor].CGColor;
context.nextFocusedView.layer.shadowOpacity = 1;
} completion:nil];
}
}
我还尝试了其他属性:
- (void)openView {
[backButton preferredFocusedView];
[backButton canBecomeFocused];
[backButton setNeedsFocusUpdate];
[self.view addSubView:customView];
}
- (UIView *)preferredFocusedView {
return [backButton preferredFocusedView];
}
但是这些都不起作用!
最佳答案
假设您有一个带有属性backButton(一个UIButton)的UIViewController。此按钮已正确设置,并且位于ViewController View 的可见范围之内。它还应具有为事件UIControlEventPrimaryActionTriggered定义的操作。
如果希望在显示UIViewController的 View 时使按钮聚焦,请将该按钮返回为preferredFocusedView:
- (UIView *)preferredFocusedView {
return self.backButton;
}
但是,如果您希望将其重点放在 View 生命周期或用户流中的其他时间,则可以使用其他方法。
例如,如果您希望按钮在任意时间成为焦点:请确保该按钮将通过preferredFocusedView返回,然后在焦点上下文(在简单情况下为 View Controller )上调用setNeedsFocusUpdate。
关于objective-c - tvOS : Force a button to be focused,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33958475/