I have set a gradient color to UIView
inside my UITableViewCell
. Everything works fine in the first load but after scroll, the gradient color is loaded again and change the position every time than the actual set condition. Here's my code for implementing gradient color :
what should I do?
Add Gradient Layer
func gradient(frame:CGRect,colors: [CGColor]) -> CAGradientLayer {
let layer = CAGradientLayer()
layer.frame = frame
layer.startPoint = CGPoint(x: 1.0, y: 0.0)
layer.endPoint = CGPoint(x: 0.0, y: 1.0)
layer.locations = [0.5,0.35]
layer.colors = colors
return layer
}
UITableViewCell Class
class AllOfferlistCell: UITableViewCell {
@IBOutlet weak var lbltitle: UILabel!
@IBOutlet weak var lblPopular: UILabel!
@IBOutlet weak var VwPercentage: UIView!
}
tableView cellForRowAtindexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllOfferlistCell
cell.lbltitle.text = "Index \(indexPath.row)"
if self.DataArray[indexPath.row].Flag == "1"{
cell.VwPercentage.layer.insertSublayer(gradient(frame: cell.VwPercentage.bounds, colors: [UIColor.red.cgColor,UIColor.white.cgColor]), at: 1)
cell.lblPopular.text = "POPULAR"
}else{
cell.VwPercentage.layer.insertSublayer(gradient(frame: cell.VwPercentage.bounds, colors: [UIColor.blue.cgColor,UIColor.white.cgColor]), at: 1)
cell.lblPopular.text = "50% OFF"
}
return cell
}
Output :
First time load
After scrolling