Last active
October 1, 2015 09:37
-
-
Save clmntcrl/0c3853e1730dc9444219 to your computer and use it in GitHub Desktop.
UIView, UIButton subclass with custom drawing method set on init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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