2

当 Apple TV 上的任何媒体应用程序(App Store、电影、电视节目等)中的焦点更改为 UITextView 时,它会以如下所示的效果突出显示:

在此处输入图像描述

它会弹出,出现一个阴影,然后在触控板上滚动您的拇指会为其提供有光泽的 3D 效果。它很像一个聚焦的 UIButton,或者作为聚焦视图的子视图包含的 UIImageView,如 UICollectionViewCell。

使用 UIButton,如果它使用 UIButtonType.System 类型进行初始化,则在获得焦点时它会获得焦点外观,而无需任何工作。

使用焦点视图中的 UIImageView 可以设置adjustsImageWhenAncestorFocused,并且在视图变得焦点时,它获得焦点外观。

但是 UITextView 和 UILabel 都不会在被选中时自动设置样式,或者在祖先聚焦时具有调整外观的属性。

所以我想知道我该怎么做。Apple 是否提供任何方式来显示这样的焦点文本视图?

4

1 回答 1

0

据我所知,您所描述的“光泽 3D 效果”只能通过使用 UIImageView.adjustsImageWhenAncestorIsFocused 来访问。并且几乎不可能重新创建自己。

基于此,我猜想效果是通过在焦点上用自身的 UIImage 副本覆盖视图来实现的。或者 Apple 可以访问低级 API 以获得您和我无法访问的光泽 3D 效果。

因此,请尝试以下操作:

class CustomTextView:  
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
  if (isFocused){
    if (self.imageOverlay != nil) {
    let imageView: UIImageView = self.toImageView() // many ways to achieve this. Ex: see UIGraphicsBeginImageContext()
    imageView.frame = bounds
    imageView.adjustsImageWhenAncestorIsFocused = true
    imageView.clipsToBounds = false
    self.imageOverlay = imageView
    addSubview(imageView)
    }
  }
  else{
    coordinator.addCoordinatedAnimations(... completion:{
      if (!self.isFocused){
        self.imageOverlay.removeFromSuperview()
        self.imageOverlay = nil
      }
    })
  }
  super.didUpdateFocus( in:context, with:coordinator ) 
}

UIImageView.adjustsImageWhenAncestorIsFocused 效果还包括平移时的缩放、阴影和反馈/视差。这些既不可移动也不可配置。

作为替代方案,尝试使用 TVUIKit 中的各种锁定视图。也许其中一个或多个也具有内置的光泽 3D 效果。我还没有尝试过它们。

于 2019-11-10T21:51:33.533 回答