我正在从事 tvOS 项目。该项目包含一个 UICollectionView
,每个 UICollectionViewCell
中都有一个 UIImageView
。首先,我在图像上使用了 adjustsImageWhenAncestorFocused
,因为当单元格处于焦点时它看起来不错并且我想要有“视差”效果。
当一个单元格处于焦点时,应该会在图像上弹出一个标签(用于产品的标题),这不是一个问题。但我希望标题具有良好的可读性,所以我想在图像顶部放置一个渐变层。
问题: 当我将渐变层添加到 UIImageView 时,它会在它上面但不会“粘附”在它上面。当用户执行视差操作时它应该与图像一起移动,但它停留在其位置并且原始图像确实在渐变层下方移动
代码
let gradientLayer = CAGradientLayer()
override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
gradientLayer.colors = [UIColor(red:0.00, green:1.00, blue:1.00, alpha:0.3).cgColor, UIColor(red:1.00, green:0, blue:1.00, alpha:0.3).cgColor]
gradientLayer.frame = productImageView.focusedFrameGuide.layoutFrame
gradientLayer.isHidden = true
self.productImageView.layer.addSublayer(self.gradientLayer)
if (self.isFocused){
gradientLayer.isHidden = false
}
else {
gradientLayer.isHidden = true
}
}
附加问题
这是一个错误吗?因为我仍然没有弄清楚如何解决这个问题。我应该把这个给苹果吗?
最佳答案
Apple 在 tvOS 11 中引入了 overlayContentView
。您应该将标签添加到此 View ,它会为您提供焦点!
关于Swift - 如何在 UICollectionViewCell 内的 UIImageView 上添加图层并保持视差效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853892/