tvOS:强制按钮进行聚焦

Mc.*_*ver 9 objective-c tvos

我有一个UIView按钮,并且需要在视图添加到按钮时将焦点设置在按钮上self.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];


         }

}
Run Code Online (Sandbox Code Playgroud)

我也尝试了其他属性:

- (void)openView {

        [backButton preferredFocusedView];
        [backButton canBecomeFocused];
        [backButton setNeedsFocusUpdate];

       [self.view addSubView:customView];
}

- (UIView *)preferredFocusedView {

       return [backButton preferredFocusedView];   
}
Run Code Online (Sandbox Code Playgroud)

但这些都没有奏效!

Jes*_*ers 6

假设你有一个带有属性backButton(UIButton)的UIViewController.此按钮已正确设置并位于ViewController视图的可见边界内.它还应该为事件UIControlEventPrimaryActionTriggered定义一个动作.

如果您希望在显示UIViewController的视图时聚焦按钮,请返回preferredFocusedView的按钮:

- (UIView *)preferredFocusedView {
   return self.backButton;
}
Run Code Online (Sandbox Code Playgroud)

但是如果您希望它在视图生命周期或用户流中的不同时间聚焦,那么您将使用不同的方法.

例如,如果希望按钮在任意时间聚焦:确保按钮将通过preferredFocusedView返回,然后在焦点上下文(在简单的情况下,视图控制器)上调用setNeedsFocusUpdate.


Gen*_*isa 4

这应该有效。

- (UIView *)preferredFocusedView {

       return backButton;   
}
Run Code Online (Sandbox Code Playgroud)

不返回 [backButton PreferredFocusedView],只需返回 backButton。