【问题标题】:Swift - How to add a layer on a UIImageView inside of a UICollectionViewCell and keep the parallax effectSwift - 如何在 UICollectionViewCell 内的 UIImageView 上添加图层并保持视差效果
【发布时间】:2017-01-25 14:17:36
【问题描述】:

我正在开发一个 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
    }
    
}

其他问题

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

【问题讨论】:

    标签: swift uiimageview parallax layer tvos


    【解决方案1】:

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

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

    【讨论】:

    • 有人知道在 iOS 11 功能之前手动执行此操作的方法吗?
    • 关于如何在 tvOS 中聚焦(并因此缩放)图像时添加圆角的任何想法?像任何其他视图、图标、电影海报一样。我已经搜索了两个星期了,它被剪掉了,或者更糟。
    • 关于如何在聚焦的 tvOS 单元上处理层,SA 上有很多不正确的答案。 Apple 的代码示例从未提及或使用此属性。此答案正确地将 overlayContentView 标识为相关属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2013-05-28
    • 1970-01-01
    • 2017-04-09
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多