Skip to content

Instantly share code, notes, and snippets.

@k3zi
Created June 17, 2016 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k3zi/73262e8a30710a7a96fe329ff1c1052e to your computer and use it in GitHub Desktop.
Save k3zi/73262e8a30710a7a96fe329ff1c1052e to your computer and use it in GitHub Desktop.
Masks any cells behind the section header of a .Plain UITableView
override func scrollViewDidScroll(scrollView: UIScrollView) {
super.scrollViewDidScroll(scrollView)
for cell in self.tableView.visibleCells {
let hiddenFrameHeight = scrollView.contentOffset.y + self.tableView(tableView, heightForHeaderInSection: 0) - cell.frame.origin.y
if hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height {
self.maskCell(cell, fromTopWithMargin: hiddenFrameHeight)
}
}
}
func maskCell(cell: UITableViewCell, fromTopWithMargin margin: CGFloat) {
cell.layer.mask = visibilityMaskForCell(cell, withLocation: margin/cell.frame.size.height)
cell.layer.masksToBounds = true
}
func visibilityMaskForCell(cell: UITableViewCell, withLocation location: CGFloat) -> CAGradientLayer {
let mask = CAGradientLayer()
mask.frame = cell.bounds
mask.colors = [RGB(255, a: 0.0).CGColor, RGB(255, a: 1.0).CGColor]
mask.locations = [NSNumber(float: Float(location)), NSNumber(float: Float(location))]
return mask
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment