Make UILabel focusable and tappable (tvOS)

后端 未结 4 875
我在风中等你
我在风中等你 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

提交回复
热议问题