如何在SSRadioButton中添加一点白色勾号

时间:2016-07-28 09:41:49

标签: swift

以下是我的代码。当我点击按钮时,我想在中间加一点白色勾号。我怎么能通过编程而不是使用图像来做到这一点...

import Foundation
import UIKit

@IBDesignable
class SSRadioButton: UIButton {

private var circleLayer = CAShapeLayer()
private var fillCircleLayer = CAShapeLayer()
override var selected: Bool {
    didSet {
        toggleButon()
    }
}
/**
    Color of the radio button circle. Default value is 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 {
    get {
        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 the 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()
}
}

1 个答案:

答案 0 :(得分:0)

这样的东西?

enter image description here

你可以通过使用UIBezierPath来绘制一些勾选的行来实现。除非你在寻找更奇特或曲线的东西?这个问题的答案是:Draw a line with UIBezierPath  有一个很好的小功能,简化了绘制线条的过程。