[Swift]UISwitchButton
UISwitchButton
class UISwitchButton: UIButton { let onImage = UIImage(named: "check-on") let offImage = UIImage(named: "check-off") var isTapped: Bool = false { didSet { if isTapped { self.setImage(onImage, forState: .Normal) } else { self.setImage(offImage, forState: .Normal) } } } override init(frame: CGRect) { super.init(frame: frame) self.isTapped = false self.setImage(offImage, forState: .Normal) self.addTarget(self, action: #selector(UISwitchButton.tapButton(_:)), forControlEvents: UIControlEvents.TouchUpInside) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func awakeFromNib() { self.addTarget(self,action:#selector(UISwitchButton.tapButton(_:)), forControlEvents: UIControlEvents.TouchUpInside) self.isTapped = false } func tapButton(sender: UIButton) { if sender == self { isTapped = !isTapped } } }