swift - How can I add a little white tick to SSRadioButton -
the following code. want add little white tick middle when clicked button. how can programming not using image...
import foundation import uikit @ibdesignable class ssradiobutton: uibutton { private var circlelayer = cashapelayer() private var fillcirclelayer = cashapelayer() override var selected: bool { didset { togglebuton() } } /** color of radio button circle. default value uicolor red. */ @ibinspectable var circlecolor: uicolor = uicolor.redcolor() { didset { circlelayer.strokecolor = circlecolor.cgcolor self.togglebuton() } } /** radius of radiobutton circle. */ @ibinspectable var circleradius: cgfloat = 5.0 @ibinspectable var cornerradius: cgfloat { { return layer.cornerradius } set { layer.cornerradius = newvalue layer.maskstobounds = newvalue > 0 } } private func circleframe() -> cgrect { var circleframe = cgrect(x: 0, y: 0, width: 2*circleradius, height: 2*circleradius) circleframe.origin.x = 0 + circlelayer.linewidth circleframe.origin.y = bounds.height/2 - circleframe.height/2 return circleframe } required init?(coder adecoder: nscoder) { super.init(coder: adecoder) initialize() } override init(frame: cgrect) { super.init(frame: frame) initialize() } private func initialize() { circlelayer.frame = bounds circlelayer.linewidth = 2 circlelayer.fillcolor = uicolor.clearcolor().cgcolor circlelayer.strokecolor = circlecolor.cgcolor layer.addsublayer(circlelayer) fillcirclelayer.frame = bounds fillcirclelayer.linewidth = 2 fillcirclelayer.fillcolor = uicolor.clearcolor().cgcolor fillcirclelayer.strokecolor = uicolor.clearcolor().cgcolor layer.addsublayer(fillcirclelayer) self.titleedgeinsets = uiedgeinsetsmake(0, (4*circleradius + 4*circlelayer.linewidth), 0, 0) self.togglebuton() } /** toggles selected state of button. */ func togglebuton() { if self.selected { fillcirclelayer.fillcolor = circlecolor.cgcolor } else { fillcirclelayer.fillcolor = uicolor.clearcolor().cgcolor } } private func circlepath() -> uibezierpath { return uibezierpath(ovalinrect: circleframe()) } private func fillcirclepath() -> uibezierpath { return uibezierpath(ovalinrect: cgrectinset(circleframe(), 2, 2)) } override func layoutsubviews() { super.layoutsubviews() circlelayer.frame = bounds circlelayer.path = circlepath().cgpath fillcirclelayer.frame = bounds fillcirclelayer.path = fillcirclepath().cgpath self.titleedgeinsets = uiedgeinsetsmake(0, (2*circleradius + 4*circlelayer.linewidth), 0, 0) } override func prepareforinterfacebuilder() { initialize() } }
something this?
you achieve using uibezierpath
draw couple of lines make tick. unless looking more fancy or curvy? answer question: draw line uibezierpath has nice little function simplifies process of drawing lines.
Comments
Post a Comment