Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Last active April 7, 2019 19:37
Show Gist options
  • Save ytyubox/0b41f1b8de68bd59d9683d81d1dfe94c to your computer and use it in GitHub Desktop.
Save ytyubox/0b41f1b8de68bd59d9683d81d1dfe94c to your computer and use it in GitHub Desktop.
class Button:UIButton{
typealias Handler = (Button)->Void
var title:String = "default title"{
didSet {
setTitle("Default Title", for: [])
sizeToFit()
}
}
var tapHandle:Handler!
var longHandle:Handler?
var longPressTime:TimeInterval{
get { return longGesture.minimumPressDuration }
set { longGesture.minimumPressDuration = newValue}
}
private
var longGesture:UILongPressGestureRecognizer!
convenience init(type:UIButton.ButtonType = .custom,
defaultEvent event:UIControl.Event = .touchUpInside,
handle:@escaping Handler) {
self.init(type:.custom)
addTarget(self, action: #selector(tapped), for: event)
longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
addGestureRecognizer(longGesture)
self.tapHandle = handle
setTitle("Default Title", for: [])
sizeToFit()
translatesAutoresizingMaskIntoConstraints = false
}
@objc
private func tapped(){
tapHandle(self)
}
@objc
private func long(){
longHandle?(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment