ios - 在 tvOS 上显示 UITextView 的焦点效果?

标签 ios uikit uitextview tvos focus-engine

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

enter image description here

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

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

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

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

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

最佳答案

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

基于此,我猜想效果是通过在焦点上用自身的 UIImage 副本覆盖 View 来实现的。或者 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 中的各种锁定 View 。也许其中一个或多个也具有内置的光泽 3D 效果。我还没有尝试过它们。

关于ios - 在 tvOS 上显示 UITextView 的焦点效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874413/

相关文章:

objective-c - 为 REST API 开发 iOS SDK

ios - 带有 map +列表的类似四方的 ScrollView

iphone - UIbutton 标签属性

objective-c - 在执行需要在主线程上执行的大量操作时,避免阻塞 UI

ios - 彩色 UITextview 的自动更正问题

ios - 使用变量更新 View

ios - 快速 xcode 错误 : "instance member ' name' cannot be used on type 'clothing_category'

iphone - 如何在 UITableViewCell 中嵌套 UITextField?

ios - 运行另一个textViewDidChange

ios - UITextView 放置在自动布局中表现得很奇怪