当 Apple TV 上的任何媒体应用程序(App Store、电影、电视节目等)中的焦点更改为 UITextView 时,它会以如下所示的效果突出显示:
它会弹出,出现一个阴影,然后在触控板上滚动您的拇指即可获得光泽的 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/