Skip to content

Instantly share code, notes, and snippets.

@clmntcrl
Last active October 1, 2015 09:37
Show Gist options
  • Save clmntcrl/0c3853e1730dc9444219 to your computer and use it in GitHub Desktop.
Save clmntcrl/0c3853e1730dc9444219 to your computer and use it in GitHub Desktop.
UIView, UIButton subclass with custom drawing method set on init
// DrawingView by Clément Cyril - @clmntcrl - http://clmntcrl.io/
// Copyright 2015 Clément Cyril.
import UIKit
class DrawingView: UIView {
var draw: CGRect -> Void = { _ in } {
didSet { setNeedsDisplay() }
}
convenience init(frame: CGRect, draw drawingClosure: CGRect -> Void) {
self.init(frame: frame)
draw = drawingClosure
}
override func drawRect(rect: CGRect) {
draw(rect)
}
}
class DrawingButton: UIButton {
var draw: CGRect -> Void = { _ in } {
didSet { setNeedsDisplay() }
}
convenience init(frame: CGRect, draw drawingClosure: CGRect -> Void) {
self.init(frame: frame)
draw = drawingClosure
}
override func drawRect(rect: CGRect) {
draw(rect)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment