ndduong ndduong - 6 months ago 20
Swift Question

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!

Answer

What you are doing for shimmering the UIImageView is wrong.

  1. You never add shimmer to the view as a subview.
  2. You don't set the UIImageView as the contentView for the shimmer

Update your code with

@IBOutlet var imageView: UIImageView!

var shimmer: FBShimmeringView!

override func viewDidLoad() {
    shimmer = FBShimmeringView(frame: self.imageView.frame)
    shimmer.contentView = imageView
    self.view.addSubView(shimmer)
    shimmer.shimmering = true
}