Skip to content

Instantly share code, notes, and snippets.

@rafattouqir
Created January 23, 2020 05:20
Show Gist options
  • Save rafattouqir/6b1d2e8f25156c285ec4688b58325255 to your computer and use it in GitHub Desktop.
Save rafattouqir/6b1d2e8f25156c285ec4688b58325255 to your computer and use it in GitHub Desktop.
protocol LoaderViewType: UIView {
func startAnimating()
func stopAnimating()
}
class LoaderButton: UIButton, LoaderViewType {
fileprivate let activityLoader: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView(style: .gray)
indicator.translatesAutoresizingMaskIntoConstraints = false
indicator.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
return indicator
}()
private static let buttonWidth: CGFloat = 30
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupView()
}
private func setupView(){
let contentView = self
contentView.addSubview(activityLoader)
activityLoader.boundInside(superView: contentView, unpin: .left)
}
func startAnimating(){
self.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: (type(of: self)).buttonWidth)
activityLoader.startAnimating()
}
func stopAnimating(){
self.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
activityLoader.stopAnimating()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment