Swift - 如何在 UICollectionViewCell 内的 UIImageView 上添加图层并保持视差效果

标签 swift uiimageview parallax layer tvos

我正在从事 tvOS 项目。该项目包含一个 UICollectionView,每个 UICollectionViewCell 中都有一个 UIImageView。首先,我在图像上使用了 adjustsImageWhenAncestorFocused,因为当单元格处于焦点时它看起来不错并且我想要有“视差”效果。

当一个单元格处于焦点时,应该会在图像上弹出一个标签(用于产品的标题),这不是一个问题。但我希望标题具有良好的可读性,所以我想在图像顶部放置一个渐变层。

问题: 当我将渐变层添加到 UIImageView 时,它会在它上面但不会“粘附”在它上面。当用户执行视差操作时它应该与图像一起移动,但它停留在其位置并且原始图像确实在渐变层下方移动

问题截图 The actual problem

代码

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 ,它会为您提供焦点!

有关更多信息,请阅读 https://developer.apple.com/documentation/uikit/uiimageview/2882128-overlaycontentview?changes=latest_minor

关于Swift - 如何在 UICollectionViewCell 内的 UIImageView 上添加图层并保持视差效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853892/

相关文章:

ios - 在不添加联系人的情况下使用 CNContactViewController (swift)

ios - 我们可以在 Swift 的 Enum 中使用初始值设定项吗?

ios - 表格 View 显示在屏幕下方

objective-c - 创建 UIImage 抠图

javascript - 如何使用 react-spring 创建视差固定 header

ios - 如何根据确认对话框防止SegmentedControl索引更改?

ios - 设置UIButton图像的内容模式

ios - 删除多个同名的 UIImageView

jquery - 跨多个类的视差效果失败

css - IE8 背景大小问题