Skip to content

Instantly share code, notes, and snippets.

@pstasiak
Created July 10, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstasiak/44ebbacd3b6c5514587ca655cbc7dba6 to your computer and use it in GitHub Desktop.
Save pstasiak/44ebbacd3b6c5514587ca655cbc7dba6 to your computer and use it in GitHub Desktop.
Adds to UIButton selecting / deselecting on touch up feature. Useful for checkmark / agreements buttons.
extension UIButton {
var isTogglingSelection: Bool {
get {
return target(forAction: #selector(_touchedInside), withSender: self) != nil
}
set {
if isTogglingSelection {
addTarget(self, action: #selector(_touchedInside), for: .touchUpInside)
} else {
removeTarget(self, action: #selector(_touchedInside), for: .touchUpInside)
}
}
}
@objc private func _touchedInside(_ sender: UIButton) {
isSelected = !isSelected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment