derdida derdida - 2 months ago 24
iOS Question

Add Transparent Gradient Mask to Cell

I would like to add a gradient mask instead of transparent to a color to a cell.

This should fade out the last line of text. So I added a UIView to my cells, and tried with:

let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = cell.gradientFooterView.frame
gradient.colors = [UIColor.clearColor().CGColor, UIColor(rgba: item.color).CGColor]
cell.gradientFooterView.layer.insertSublayer(gradient, atIndex: 0)


But with no success. How can I create a transparent mask?

Answer

it seems you miss locations, starPoint and endPoint of the gradient you can modify this piece of code accordly to your need

var maskLayer = CAGradientLayer()
maskLayer.frame = self.collectionView.bounds
let black = UIColor.whiteColor().CGColor
let clear = UIColor(white: 1, alpha: 0).CGColor
maskLayer.colors = [black, clear, clear, black]
maskLayer.locations = [0.0, 0.15, 0.85, 1.0]
maskLayer.startPoint = CGPointMake(0.0, 0.0)
maskLayer.endPoint = CGPointMake(1.0, 0.0)
yourview.layer.addSublayer(maskLayer)