Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafattouqir/f97d7d26557909ef06a005411b0a9e34 to your computer and use it in GitHub Desktop.
Save rafattouqir/f97d7d26557909ef06a005411b0a9e34 to your computer and use it in GitHub Desktop.
PKHUD custom loader with cancel button and action on cancel button click
class LoaderWithCancelView: UIView,PKHUDAnimating{
let progressView: PKHUDProgressView = {
let pkHUDPV = PKHUDProgressView()
pkHUDPV.translatesAutoresizingMaskIntoConstraints = false
return pkHUDPV
}()
let buttonCancel:UIButton = {
let button = UIButton()
button.setTitle("Cancel", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor.black.withAlphaComponent(0.3)
button.translatesAutoresizingMaskIntoConstraints = false
// button.heightAnchor.constraint(equalToConstant: 30).isActive = true
return button
}()
let pKHUDFrame = CGRect(origin: CGPoint.zero, size: CGSize(width: 156.0, height: 156.0))
var onCancelButtonTapp : (() -> Swift.Void)?
override init(frame: CGRect) {
super.init(frame: pKHUDFrame)
setupSubviews()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupSubviews()
}
func setupSubviews(){
buttonCancel.addTarget(self, action: #selector(buttonCancelTapped), for: .touchUpInside)
addSubview(progressView)
addSubview(buttonCancel)
let contentView = self
progressView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
progressView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
progressView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
progressView.bottomAnchor.constraint(equalTo: buttonCancel.topAnchor, constant: 0).isActive = true
buttonCancel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
buttonCancel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
buttonCancel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
}
public func startAnimation() {
progressView.startAnimation()
}
public func stopAnimation() {
progressView.stopAnimation()
}
//action
@objc func buttonCancelTapped() {
onCancelButtonTapp?()
}
}
let customHudWithButton = LoaderWithCancelView()
customHudWithButton.onCancelButtonTapp = {
// on click action
print("Cancel tapped")
}
PKHUD.sharedHUD.contentView = customHudWithButton
PKHUD.sharedHUD.show()
PKHUD.sharedHUD.hide(afterDelay: 2.0) { success in
// Completion Handler
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment