你如何将Facebook闪烁添加到图像和UIViews?

时间:2016-06-05 05:22:34

标签: ios swift facebook ios-animations

我正在尝试Facebook闪烁的库,我能够使用此代码为文本标签添加闪烁。

    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

但是,我无法制作图像或UIImage微光。这甚至可能吗?谢谢!

4 个答案:

答案 0 :(得分:6)

你正在做什么来闪烁UIImageView是错误的。

  1. 您没有在视图中添加微光作为子视图。
  2. 您没有将UIImageView设置为闪烁的contentView
  3. 使用类似的内容更新您的代码:

    @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
    }
    

答案 1 :(得分:1)

Swift 3 with Shimmer(1.0.2):

var imageView: UIImageView!

var shimmer: FBShimmeringView!

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    shimmer = FBShimmeringView(frame: self.imageView.frame)
    shimmer.contentView = imageView
    self.view.addSubview(shimmer)
    shimmer.isShimmering = true
}

答案 2 :(得分:0)

@Shubhank回答我有问题,尽管答案很好。

使用viewDidLoad代替viewDidLayoutSubviews用于闪光框架并没有给我想要的结果。

所以我更新了代码,一切正常。

@IBOutlet var imageView: UIImageView!

var shimmer: FBShimmeringView!

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

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

答案 3 :(得分:0)

您还可以使用约束,而不必在每次视图布局子视图时都初始化微光视图:

let shimmer = FBShimmeringView(frame: .zero)
view.addSubview(shimmer)
shimmer.translatesAutoresizingMaskIntoConstraints = false
let leadingConstraint = NSLayoutConstraint(item: shimmer, attribute: .leading, relatedBy: .equal, toItem: navLabel, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: shimmer, attribute: .trailing, relatedBy: .equal, toItem: navLabel, attribute: .trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: shimmer, attribute: .top, relatedBy: .equal, toItem: navLabel, attribute: .top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: shimmer, attribute: .bottom, relatedBy: .equal, toItem: navLabel, attribute: .bottom, multiplier: 1, constant: 0)
view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])