Search code examples
iosswiftfacebookios-animations

How do you add Facebook shimmering to images and UIViews?


I'm trying out Facebook shimmering library and I was able to add shimmering to text label using this code.

    self.shimmeringView = FBShimmeringView(frame: CGRectMake(0, 0, 200, (self.navigationController?.navigationBar.bounds.height)!))
    self.shimmeringView.contentView = navLabel
    self.navigationItem.titleView = self.shimmeringView
    self.shimmeringView.shimmering = true

However, I am unable to make an image or an UIImage shimmer. Is this even possible? Thanks!


Solution

  • What you are doing for shimmering the UIImageView is wrong.

    1. You did not add shimmer to the view as a subview.
    2. You did not set the UIImageView as the contentView for the shimmer.

    Update your code with something like that:

    @IBOutlet var imageView: UIImageView!
    
    var shimmer: FBShimmeringView!
    
    override func viewDidLoad() {
        shimmer = FBShimmeringView(frame: self.imageView.frame)
        shimmer.contentView = imageView
        self.view.addSubview(shimmer)
        shimmer.isShimmering = true
    }