// // LoadingBar.swift // import UIKit @IBDesignable public class LoadingBar: UIView { @IBInspectable public var progress: Float = 0.0 { didSet { UIView.animateWithDuration(0.1, animations: { () -> Void in var width = self.frame.size.width * CGFloat(self.progress) self.innerBar?.frame = CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: width, height: self.frame.size.height) if self.progress >= 1.0 { self.alpha = 0.0 } else { self.alpha = 1.0 } }) } } @IBInspectable var color: UIColor? { didSet { self.innerBar?.backgroundColor = self.color! } } var innerBar: UIView? override public func layoutSubviews() { super.layoutSubviews() self.userInteractionEnabled = false self.backgroundColor = UIColor.clearColor() if self.color == nil { self.color = self.tintColor } if innerBar == nil { var width = self.frame.size.width * CGFloat(self.progress) self.innerBar = UIView(frame: CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: width, height: self.frame.size.height)) self.innerBar?.backgroundColor = self.color self.addSubview(self.innerBar!) } } }