我的 iOS 项目中有一个 tableview,它使用图像作为背景。图像不滚动,它是静态的。因此,我也有透明的单元格和节标题。现在我的问题是如何使(透明)单元格在(也是透明的)部分标题后面“隐藏”或“消失”?
是否可以?
最佳答案
在您的自定义单元格上
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)
}
}
}
}
关于ios - 是否可以在透明部分标题后面 "hide"UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49498292/