Add Facebook Shimmer on multiple UIViews

前端 未结 2 1155
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 09:29

I am trying to add Facebook Shimmer on UICollectionViewCell which has multiple UIViews.

For one UIView, it\'s working fine with below code:

<
2条回答
  •  抹茶落季
    2021-01-13 10:22

    Below extension is working fine.

    extension UIView {
        func startShimmeringViewAnimation() {
            let gradientLayer = CAGradientLayer()
            gradientLayer.frame = self.bounds
            gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
            gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
            let gradientColorOne = UIColor(white: 0.90, alpha: 1.0).cgColor
            let gradientColorTwo = UIColor(white: 0.95, alpha: 1.0).cgColor
            gradientLayer.colors = [gradientColorOne, gradientColorTwo, gradientColorOne]
            gradientLayer.locations = [0.0, 0.5, 1.0]
            self.layer.addSublayer(gradientLayer)
    
            let animation = CABasicAnimation(keyPath: "locations")
            animation.fromValue = [-1.0, -0.5, 0.0]
            animation.toValue = [1.0, 1.5, 2.0]
            animation.repeatCount = .infinity
            animation.duration = 1.25
            gradientLayer.add(animation, forKey: animation.keyPath)
        }
    }
    

    In UITableViewCell class, we need to add the Shimmer for each views.

    class UAShimmerCell: UITableViewCell {
        @IBOutlet weak var thumbNailView: UIView!
        @IBOutlet weak var label1View: UIView!
        @IBOutlet weak var label2View: UIView!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            thumbNailView.startShimmeringViewAnimation()
            label1View.startShimmeringViewAnimation()
            label2View.startShimmeringViewAnimation()
        }
    }
    

提交回复
热议问题