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

Sti*_*ijn 4 layer uiimageview parallax swift tvos

我正在做一个 tvOS 项目。这个项目包含了UICollectionViewUIImageView每一个内部UICollectionViewCell。首先,我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
    }
    
}
Run Code Online (Sandbox Code Playgroud)

补充问题

这是一个错误吗?因为我仍然不知道如何解决这个问题。我应该把这个给苹果吗?

Jer*_*ker 5

AppleoverlayContentView在 tvOS 11 中引入。你应该将你的标签添加到这个视图中,它会为你做焦点!

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