Skip to content

Instantly share code, notes, and snippets.

@strzempa
Created October 11, 2019 08:13
Show Gist options
  • Save strzempa/da0ec6b3b6bc7acff109f047d1525e20 to your computer and use it in GitHub Desktop.
Save strzempa/da0ec6b3b6bc7acff109f047d1525e20 to your computer and use it in GitHub Desktop.
import UIKit
final class CheckBoxSingleselectButton: UIExpandedTouchAreaButton {
private var shapeLayerRect: CGRect {
let width = bounds.width / 1.4
let height = bounds.height / 1.4
return CGRect(x: (bounds.width - width) / 2,
y: (bounds.height - height) / 2,
width: width,
height: height)
}
private var blueShapeLayer: CAShapeLayer {
let layer = CAShapeLayer()
layer.path = UIBezierPath(roundedRect: shapeLayerRect, cornerRadius: 3).cgPath
layer.fillColor = UIColor.lightRoyalBlue.cgColor
return layer
}
private var whiteShapeLayer: CAShapeLayer {
let layer = CAShapeLayer()
layer.path = UIBezierPath(roundedRect: shapeLayerRect, cornerRadius: 3).cgPath
layer.fillColor = UIColor.white.cgColor
return layer
}
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = .white
layer.cornerRadius = 5
tintColor = .clear
}
override var isSelected: Bool {
didSet {
if isSelected {
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor
backgroundColor = .white
layer.addSublayer(blueShapeLayer)
} else {
layer.borderWidth = 1
layer.borderColor = UIColor.grey.cgColor
backgroundColor = .white
layer.addSublayer(whiteShapeLayer)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment