tvOS:强制按钮进行聚焦

时间:2015-11-27 13:28:20

标签: 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];


         }

}

我还尝试了其他属性:

- (void)openView {

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

       [self.view addSubView:customView];
}

- (UIView *)preferredFocusedView {

       return [backButton preferredFocusedView];   
}

但这些都没有奏效!

3 个答案:

答案 0 :(得分:6)

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

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

protected void btnChangePassword_OnClick(object sender, EventArgs eventArgs)
{
    //Changes Password of current Logged in User
    string oldPass = txtCurrentPass.Text;
    string newPass = txtNewPassword.Text;
    MembershipUser user = Membership.GetUser(User.Identity.Name);

    if (txtCurrentPass.Text == txtNewPassword.Text)
    {
        lblPassChange.Visible = true;
        lblPassChange.Text = "Old Password must not match New Password";
    }
    else
    {
        if (user != null && user.ChangePassword(oldPass, newPass))
        {
            lblPassChange.Visible = true;
            lblPassChange.Text = "Password Changed Successfully";
            lblPassChange.CssClass = "label label-success";
        }
        else
        {
            lblPassChange.Visible = true;
            lblPassChange.Text = "Error, Please try again";
        }
    }
}

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

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

答案 1 :(得分:4)

这应该有用。

- (UIView *)preferredFocusedView {

       return backButton;   
}

而不是返回[backButton preferredFocusedView],只需返回backButton。

答案 2 :(得分:0)

您需要从preferredFocusView返回您想要关注的视图并致电setNeedsFocusUpdate,以便焦点系统将焦点转移到它。