Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created September 19, 2017 20:15
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 StanislavK/3e139984381cff548778d3934bab2d20 to your computer and use it in GitHub Desktop.
Save StanislavK/3e139984381cff548778d3934bab2d20 to your computer and use it in GitHub Desktop.
import UIKit
// Credits to https://github.com/JohnSundell/SwiftKit/blob/master/Source/iOS/ClosureButton.swift
/// A button that executes a closure when tapped
public class ClosureButton: UIButton {
private let closure: () -> Void
public init(frame: CGRect, closure: () -> Void) {
self.closure = closure
super.init(frame: frame)
self.addTarget(self, action: #selector(executeClosure), forControlEvents: .TouchUpInside)
}
public convenience init(closure: () -> Void) {
self.init(frame: CGRectZero, closure: closure)
}
public required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
internal func executeClosure() {
self.closure()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment