Make UILabel focusable and tappable (tvOS)

后端 未结 4 728
我在风中等你
我在风中等你 2021-01-12 20:45

I\'m trying to implement 6 lines high description label and I want it to be focusable. Ideally that would mean extending UILabel class to make a custom component. I tried th

相关标签:
4条回答
  • 2021-01-12 20:51

    Use a collection view with just one cell and add transform to cell and change cell background color in didUpdateFocusInContext when focus moves to cell.

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        coordinator.addCoordinatedAnimations({
            if self.focused {
                self.transform = CGAffineTransformMakeScale(1.01, 1.01)
                self.backgroundColor = UIColor.whiteColor()
                self.textLabel.textColor = .blackColor()
            }
            else {
                self.transform = CGAffineTransformMakeScale(1, 1)
                self.backgroundColor = UIColor.clearColor()
                self.textLabel.textColor = .whiteColor()
            }
            }, completion: nil)
    }
    

    As an additional step you could try to extract the color of the image if you are using the image as background like iTunes and use that for Visual effect view behind the cell.

    Also you can apply transform to the collectionView in the video controller to make it look like in focus

    0 讨论(0)
  • 2021-01-12 20:56

    Ok, answering my own question :)

    So it appears that some some views are "focusable" on tvOS out-of-the-box, and other have to be instructed to do so.

    I finally ended up using UITextView, which has a selectable property, but if not one of these focusable views by default. Editing of TextView has to be disabled to make it look like UILabel. Also, currently there is a bug which prevents you from using selectable property from Interface Builder but works from code.

    Naturally, canBecomeFocused() and didUpdateFocusInContext had to be implemented too. You'll also need to pass a UIViewController because UITextView is not capable of presenting a modal view controller. Bellow is what I ended up creating.

    class FocusableText: UITextView {
    
        var data: String?
        var parentView: UIViewController?
    
        override func awakeFromNib() {
            super.awakeFromNib()
            let tap = UITapGestureRecognizer(target: self, action: "tapped:")
            tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
            self.addGestureRecognizer(tap)
        }
    
        func tapped(gesture: UITapGestureRecognizer) {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let descriptionView = storyboard.instantiateViewControllerWithIdentifier("descriptionView") as? DescriptionViewController {
                if let view = parentView {
                    if let show = show {
                        descriptionView.descriptionText = self.data
                        view.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
                        view.presentViewController(descriptionView, animated: true, completion: nil)
                    }
                }
            }
        }
    
        override func canBecomeFocused() -> Bool {
            return true
        }
    
        override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    
            if context.nextFocusedView == self {
                coordinator.addCoordinatedAnimations({ () -> Void in
                    self.layer.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.2).CGColor
                }, completion: nil)
            } else if context.previouslyFocusedView == self {
                coordinator.addCoordinatedAnimations({ () -> Void in
                    self.layer.backgroundColor = UIColor.clearColor().CGColor
                }, completion: nil)
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-12 20:56

    As for making a UILabel focusable:

    class MyLabel: UILabel {
    
        override var canBecomeFocused: Bool {
            return true
        }
    
        override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
            super.didUpdateFocus(in: context, with: coordinator)
    
            backgroundColor = context.nextFocusedView == self ? .blue:.red
    
        }
    }
    

    IMPORTANT!!! As stated on the apple developer portal:

    The value of this property is true if the view can become focused; false otherwise.

    By default, the value of this property is false. This property informs the focus engine if a view is capable of being focused. Sometimes even if a view returns true, a view may not be focusable for the following reasons:

    • The view is hidden.

    • The view has alpha set to 0.

    • The view has userInteractionEnabled set to false.

    • The view is not currently in the view hierarchy.

    0 讨论(0)
  • 2021-01-12 20:59

    You can use system button, and set the background image in storyboard to an image that contains the color you would like

    0 讨论(0)
提交回复
热议问题