Created
January 23, 2020 05:20
-
-
Save rafattouqir/6b1d2e8f25156c285ec4688b58325255 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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