【问题标题】:Display focus effects for UITextView on tvOS?在 tvOS 上显示 UITextView 的焦点效果?
【发布时间】:2016-03-08 17:48:38
【问题描述】:

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

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

对于 UIButton,如果它被初始化为 UIButtonType.System 类型,它在获得焦点时获得焦点外观,无需任何工作。

在焦点视图中使用 UIImageView 可以设置adjustsImageWhenAncestorFocused,当视图成为焦点时,它会获得焦点外观。

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

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

【问题讨论】:

标签: ios uikit uitextview tvos focus-engine


【解决方案1】:

据我所知,您所描述的“光泽 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 效果。我还没有尝试过它们。

【讨论】:

  • 这可能就是为什么 Apple 这样做时它看起来是静态的。然后点击“显示更多”会打开全屏说明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多