Skip to content

Instantly share code, notes, and snippets.

@andrewbranch
Last active August 29, 2015 14:11
Show Gist options
  • Save andrewbranch/2b445dcf558a19fda9e2 to your computer and use it in GitHub Desktop.
Save andrewbranch/2b445dcf558a19fda9e2 to your computer and use it in GitHub Desktop.
Simple, elegant radio button view

RadioView

Usage

Just init with a frame or add a UIView to your nib and set its class to RadioView. (Give it a 1:1 aspect ratio if you want it to look normal.)

let radio = RadioView(frame: CGRectMake(0, 0, 30, 30))
radio.color = UIColor.redColor()
radio.selected = true

class RadioView: UIView {
var selected: Bool = false {
didSet {
UIView.animateWithDuration(self.fadeTime) {
self.inner.alpha = self.selected ? 1 : 0
}
}
}
var color: UIColor = UIColor.blackColor() {
didSet {
self.layer.borderColor = self.color.CGColor
self.inner.layer.backgroundColor = self.color.CGColor
}
}
var fadeTime: NSTimeInterval = 0
private lazy var inner: UIView = {
return UIView(frame: CGRectMake(0, 0, 0, 0))
}()
override init() {
super.init()
layout()
}
override init(frame: CGRect) {
super.init(frame: frame)
layout()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layout()
}
override func awakeFromNib() {
if let color = self.backgroundColor {
self.color = color
self.backgroundColor = nil
}
layout()
}
private func layout() {
self.layer.cornerRadius = self.frame.width / 2
self.layer.borderWidth = self.frame.width / 12
self.layer.borderColor = self.color.CGColor
self.inner.setTranslatesAutoresizingMaskIntoConstraints(false)
self.inner.addConstraint(NSLayoutConstraint(item: self.inner, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: self.frame.width * 0.6))
self.inner.addConstraint(NSLayoutConstraint(item: self.inner, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: self.frame.height * 0.6))
self.addSubview(inner)
self.addConstraint(NSLayoutConstraint(item: self.inner, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.inner, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
self.inner.layoutIfNeeded()
self.inner.layer.cornerRadius = self.inner.frame.width / 2
self.inner.layer.backgroundColor = self.layer.borderColor
self.inner.alpha = 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment