1.建立一個專案
2.建立兩個
SSRadioButton.swift與
SSRadioButtonsController.swift檔案
SSRadioButton.swift
//
// SSRadioButton.swift
// SampleProject
//
// Created by Shamas on 18/05/2015.
// Copyright (c) 2015 Al Shamas Tufail. All rights reserved.
//
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()
}
}
SSRadioButtonsController.swift
//
// RadioButtonsController.swift
// TestApp
//
// Created by Al Shamas Tufail on 24/03/2015.
// Copyright (c) 2015 Al Shamas Tufail. All rights reserved.
//
import Foundation
import UIKit
/// RadioButtonControllerDelegate. Delegate optionally implements didSelectButton that receives selected button.
@objc protocol SSRadioButtonControllerDelegate {
/**
This function is called when a button is selected. If 'shouldLetDeSelect' is true, and a button is deselected, this function
is called with a nil.
*/
optional func didSelectButton(aButton: UIButton?)
}
class SSRadioButtonsController : NSObject
{
private var buttonsArray = [UIButton]()
private weak var currentSelectedButton:UIButton? = nil
weak var delegate : SSRadioButtonControllerDelegate? = nil
/**
Set whether a selected radio button can be deselected or not. Default value is false.
*/
var shouldLetDeSelect = false
/**
Variadic parameter init that accepts UIButtons.
- parameter buttons: Buttons that should behave as Radio Buttons
*/
init(buttons: UIButton...) {
super.init()
for aButton in buttons {
aButton.addTarget(self, action: #selector(SSRadioButtonsController.pressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
self.buttonsArray = buttons
}
/**
Add a UIButton to Controller
- parameter button: Add the button to controller.
*/
func addButton(aButton: UIButton) {
buttonsArray.append(aButton)
aButton.addTarget(self, action: #selector(SSRadioButtonsController.pressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
/**
Remove a UIButton from controller.
- parameter button: Button to be removed from controller.
*/
func removeButton(aButton: UIButton) {
let iteration = 0
var iteratingButton: UIButton? = nil
for i in iteration..<buttonsArray.count {
iteratingButton = buttonsArray[i]
if(iteratingButton == aButton) {
break
} else {
iteratingButton = nil
}
}
if(iteratingButton != nil) {
buttonsArray.removeAtIndex(iteration)
iteratingButton!.removeTarget(self, action: #selector(SSRadioButtonsController.pressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
if currentSelectedButton == iteratingButton {
currentSelectedButton = nil
}
}
}
/**
Set an array of UIButons to behave as controller.
- parameter buttonArray: Array of buttons
*/
func setButtonsArray(aButtonsArray: [UIButton]) {
for aButton in aButtonsArray {
aButton.addTarget(self, action: #selector(SSRadioButtonsController.pressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
buttonsArray = aButtonsArray
}
func pressed(sender: UIButton) {
if(sender.selected) {
if shouldLetDeSelect {
sender.selected = false
currentSelectedButton = nil
}
} else {
for aButton in buttonsArray {
aButton.selected = false
}
sender.selected = true
currentSelectedButton = sender
}
delegate?.didSelectButton?(currentSelectedButton)
}
/**
Get the currently selected button.
- returns: Currenlty selected button.
*/
func selectedButton() -> UIButton? {
return currentSelectedButton
}
}
3.在
Main.storyboard拉三個button
4.連結Button至
ViewController.swift
5.選擇Buuton->點選Show the Identity inspector ->Class選
SSRadioButton
方法一:點選+ 新增circleColor與circleRadius
方法二:點選Show the Attributes inspector會出現Radio Button 去設定
6.
ViewController.swift
import UIKit
class ViewController: UIViewController, SSRadioButtonControllerDelegate {
@IBOutlet weak var bt1: UIButton!
@IBOutlet weak var bt2: UIButton!
@IBOutlet weak var bt3: UIButton!
var radioButtonController: SSRadioButtonsController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
radioButtonController = SSRadioButtonsController(buttons: bt1, bt2, bt3)
radioButtonController!.delegate = self
radioButtonController!.shouldLetDeSelect = true //true:點第二下取消 false:點第二下仍是選擇
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func didSelectButton(aButton: UIButton?) {
let currentButton = radioButtonController!.selectedButton()!
print("點選\(currentButton.currentTitle!)")
}
}
檔案下載:
https://github.com/terryyamg/Swift-RadioButtonTest
參考連結:
https://github.com/shamasshahid/SSRadioButtonsController