简体   繁体   English

当单元格即将离屏时,UICollectionView Cell的阴影突然消失

[英]Shadow of UICollectionView Cell suddenly disappears when the cell is about to go offscreen

I have a collection view with rounded and shadow-dropped cells. 我有一个带有圆角和阴影掉落单元格的集合视图。 The shadow of the cell suddenly disappears, instead of smoothly moving out of the view, when the cell is about to be covered by the navigation bar. 当单元格即将被导航栏覆盖时,单元格的阴影突然消失,而不是平滑地移出视图。 Below is the code: 以下是代码:

    contentView.layer.cornerRadius = 20
    contentView.layer.masksToBounds = true

    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOpacity = 0.1
    layer.shadowOffset = CGSize(width: 0, height: 5.0)
    layer.shadowRadius = 5.0
    layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius - 3).cgPath
    layer.shouldRasterize = true
    layer.rasterizationScale = UIScreen.main.scale

Here is the gif showing the problem. 这是显示问题的GIF。 Focus on the shadow at the bottom of the cell. 专注于细胞底部的阴影。 The navigation bar is white. 导航栏为白色。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you! 谢谢!

问题演示(gif)

I believe it's simply because the cell does really get removed from the view, thus it's shadow goes with it. 我相信这只是因为单元确实从视图中移除了,因此它的阴影随之而来。 At that point the frame of the cell is out of the view, while shadow is still visible (shadow is around the frame) - but when the frame goes out of view, the collectionView removes the cell. 此时,单元格的框架在视图之外,而阴影仍然可见(阴影在框架周围) - 但是当框架离开视图时, collectionView会移除单元格。 And so the shadow disappears abruptly. 所以阴影突然消失了。

What I would do about it is very simple. 我会怎么做才非常简单。 I would refactor the code to wrap the current cell contents into a new UIView - lets call it wrapper , that will drop the shadow. 我会重构代码以将当前单元格内容包装到一个新的UIView - 让我们调用它wrapper ,这将丢弃阴影。 Then put this wrapper into the cell's contentView so that the wrapper along with its shadow will fit inside of the contentView . 然后将此wrapper放入单元格的contentView以便wrapper及其阴影适合contentView内部。 Then the shadow will become a part of the contentView frame, which means that the shadow will disappear from the screen only when the whole cell is hidden. 然后阴影将成为contentView框架的一部分,这意味着只有当整个单元格被隐藏时,阴影才会从屏幕上消失。

Of course, you will have to modify the collectionView 's size for cell, because now the cells will be bigger to contain also the shadow. 当然,您必须修改collectionView的单元格大小,因为现在单元格将更大以包含阴影。

Instead of setting shadow of contentView embed all your content in a UIView and set shadow of that view instead. 而不是设置contentView阴影,而是将所有内容嵌入到UIView并设置该视图的阴影。 Then you have to increase the height of your cell about 20 points or as much your shadow height. 然后你必须增加你的细胞高度大约20 points或你的阴影高度。 That should work I guess. 那应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM