是否可以将UITableViewCell隐藏在透明的section header后面?

8

我在iOS项目中使用了一个带有背景图片的tableview,该图像不会滚动,是静态的。因此,我还有透明的单元格和部分标题。
现在我的问题是如何使(透明的)单元格“隐藏”或“消失”在(同样透明的)部分标题后面?
这可能吗?

Screenshot


你介意将TableView切换为分组的TableView吗?这样,表头会随着单元格一起滚动,而不是它们互相叠加?因为从技术上讲,这将解决此问题。 - Kayla Galway
是的,但让我们把它作为最后的手段 :) 谢谢你的回答。 - Paweł Zgoda-Ferchmin
这看起来有一些好的想法:https://dev59.com/Nmct5IYBdhLWcg3wZMjn?rq=1 - ghostatron
你需要为所有位于标题视图下方的单元格应用遮罩。 在scrollViewDidScroll上使用。教程:https://medium.com/@peteliev/layer-masking-for-beginners-c18a0a10743 - panychyk.dima
1个回答

7
在您的自定义单元格上
public func maskCell(fromTop margin: CGFloat) {
    layer.mask = visibilityMask(withLocation: margin / frame.size.height)
    layer.masksToBounds = true
}

private func visibilityMask(withLocation location: CGFloat) -> CAGradientLayer {
    let mask = CAGradientLayer()
    mask.frame = bounds
    mask.colors = [UIColor.white.withAlphaComponent(0).cgColor, UIColor.white.cgColor]
    let num = location as NSNumber
    mask.locations = [num, num]
    return mask
}

并且在你的ViewController UIScrollViewDelegate

func scrollViewDidScroll(_ scrollView: UIScrollView) {
     for cell in self.lessonsTableView.visibleCells {
         let paddingToDisapear = CGFloat(25)

         let hiddenFrameHeight = scrollView.contentOffset.y + paddingToDisapear - cell.frame.origin.y
         if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {
             if let customCell = cell as? LessonTableViewCell {
                 customCell.maskCell(fromTop: hiddenFrameHeight)
             }
         }

     }
 }

请不要仅发布代码作为答案,还需提供解释您的代码是如何解决问题的。带有解释的答案通常质量更高,更容易获得赞同。 - Mark Rotteveel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接