Swift 单元内UIImageView子视图的tvOS视差效应

Swift 单元内UIImageView子视图的tvOS视差效应,swift,uiimageview,focus,uicollectionviewcell,tvos,Swift,Uiimageview,Focus,Uicollectionviewcell,Tvos,我有一个带有UICollectionView的tvOS应用程序。在每个单元格内都有一个ui图像视图,该视图将AdjustSimageWhennancestorFocused设置为true,在单元格对焦时为我提供了良好的视差效果 我试图在图像上方添加一个横幅,并将其“粘贴”到该图像上,以便视差也应用到该图像上 我尝试过将其添加为子视图,将其添加为子层,处理约束,但没有任何效果。无论何时细胞被聚焦,图像都会产生效果,横幅保持静止。现在无法将其添加到UIImageView中,同时获得苹果的视差效果 因

我有一个带有
UICollectionView
的tvOS应用程序。在每个单元格内都有一个
ui图像视图
,该视图将
AdjustSimageWhennancestorFocused
设置为
true
,在单元格对焦时为我提供了良好的视差效果

我试图在图像上方添加一个横幅,并将其“粘贴”到该图像上,以便视差也应用到该图像上


我尝试过将其添加为子视图,将其添加为子层,处理约束,但没有任何效果。无论何时细胞被聚焦,图像都会产生效果,横幅保持静止。

现在无法将其添加到UIImageView中,同时获得苹果的视差效果

因此,您必须以编程方式实现效果,或者使用CGContext绘制图像并在其上添加横幅

我正在做的是在collectionViewCell中使用圆形图像,同时保留苹果自己的视差效果。这有点像这样:

imageView.sd_setImage(with: URL(string:url), placeholderImage: UIImage(named: "placeholder"), options: SDWebImageOptions.avoidAutoSetImage) { [weak self] (image, error, cacheType, url) in
    if let image = image {
        if let strongself = self {
            UIGraphicsBeginImageContext(strongself.imageView.frame.size)
            if let ctx = UIGraphicsGetCurrentContext() {
                let layer = CALayer()
                layer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: strongself.imageView.frame.size)
                layer.cornerRadius = 8
                layer.masksToBounds = true
                layer.contentsGravity = "resizeAspectFill"
                layer.contents = image.cgImage
                layer.render(in: ctx)
                let processedImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                strongself.imageView.image = processedImage
            }
        }
    }
}

确保在需要时将内容移动到后台线程,并且在加载图像时单元格可能已被重用。因此,在loadImage completion处理程序中为唯一id添加一个检查。

现在无法将其添加到UIImageView中,同时也可以获得苹果的视差效果

因此,您必须以编程方式实现效果,或者使用CGContext绘制图像并在其上添加横幅

我正在做的是在collectionViewCell中使用圆形图像,同时保留苹果自己的视差效果。这有点像这样:

imageView.sd_setImage(with: URL(string:url), placeholderImage: UIImage(named: "placeholder"), options: SDWebImageOptions.avoidAutoSetImage) { [weak self] (image, error, cacheType, url) in
    if let image = image {
        if let strongself = self {
            UIGraphicsBeginImageContext(strongself.imageView.frame.size)
            if let ctx = UIGraphicsGetCurrentContext() {
                let layer = CALayer()
                layer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: strongself.imageView.frame.size)
                layer.cornerRadius = 8
                layer.masksToBounds = true
                layer.contentsGravity = "resizeAspectFill"
                layer.contents = image.cgImage
                layer.render(in: ctx)
                let processedImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                strongself.imageView.image = processedImage
            }
        }
    }
}

确保在需要时将内容移动到后台线程,并且在加载图像时单元格可能已被重用。因此,在loadImage completion处理程序中为唯一id添加一个检查。

您也可以通过编程实现此效果。我想看看是否有更简短、更简单的解决方案。以编程方式实现效果将是我最后的选择。@AndraTodorescu您能发布最终解决方案的代码吗?我也面临同样的问题,你也可以通过编程来实现这个效果。我想看看是否有一个更短、更简单的解决方案。以编程方式实现效果将是我最后的选择。@AndraTodorescu您能发布最终解决方案的代码吗?我也面临同样的问题谢谢!它就像一个符咒!我刚刚删除了所有与设置角半径相关的内容,我创建了我的视图,设置了框架,然后像这样添加了:layer.addSublayer(myCustomView.layer)谢谢!它就像一个符咒!我刚刚删除了与设置角半径相关的所有内容,创建了视图,设置了框架,然后像这样添加了:layer.addSublayer(myCustomView.layer)