Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "hide" a UITableViewCell behind a transparent section header?

Tags:

xcode

ios

swift

I have a tableview in my iOS project that uses an image as background. The image does not scroll, it is static. Because of that I also have transparent cells and section headers.
Now my question is how can I make the (transparent) cells to "hide" or "disappear" behind the (also transparent) section header?
Is it possible?

Screenshot

like image 643
Paweł Zgoda-Ferchmin Avatar asked Mar 26 '18 18:03

Paweł Zgoda-Ferchmin


1 Answers

On your custom cell

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
}

and on you 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)
             }
         }

     }
 }
like image 146
rodrigo camargo kopichinski Avatar answered Nov 06 '22 23:11

rodrigo camargo kopichinski