PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 29, 2022

[FIXED] How to detect button highlight on Apple TV in Swift

 September 29, 2022     apple-tv, swift, tvos, uibutton, xcode     No comments   

Issue

I am making a simple app that will show the user some information when they hover over a button. I have looked all over for an answer for this but it seems that no one has wondered this yet. I know it is possible to detect button highlights because I have seen it in some apps I downloaded on my Apple TV. Here is basically what I'm aiming for:

@IBAction func firstButton(_ sender: Any) {
//This function would be called when the first button is highlighted/hovered over

label.text = "Hi there"

 }

@IBAction func secondButton(_ sender: Any) {
//This function would be called when the second button is highlighted/hovered over

label.text = "How are you?"

 }

I know that just creating an IBAction func won't do the trick, but I am just using this as an example of what I want to do.

So is there a way to detect button highlights/button hovering and how?

Thanks in advance. Any help is appreciated.


Solution

By hover, I think you mean "is the button in focus". There are a couple ways to tell if a UI element is in focus.

For UIViewController you can override didUpdateFocus which provides context about what happening in the focus engine.

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if context.nextFocusedView == myButton {
         print("My button is about to be focused")
    }
    else {
         print("My button is NOT in focus")
   }
}

Or in a custom UIView (ie. a customUIButton) you can check the isFocused property.

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    print(isFocused)
}


Answered By - Chris
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing