For Radio Buttons there is nothing that comes built in Xcode iOS Swift2.
Radio Buttons
Radio buttons are a special type of control that always appear in
groups; they are typically displayed as a list of options with
selectable buttons alongside. Their behaviour is also somewhat unique;
any button within the group can be selected, but selecting one button
will deselect all other buttons in the group. Only a single button,
within the same group, can be selected at one time.
Steps to start designing RadioButtons
Create new project in Xcode,Start up Xcode and go to File\New\Project. Select iOS\Application\Single View Application, and click Next.
Create a two custom classes,
NMPLRadioButton.swift
NMPLRadioButtonsController
NMPLRadioButton.swift
import Foundationimport UIKit
@IBDesignable
class NMPLRadioButton: 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()
}
}
NMPLRadioButtonsController.swift
import Foundation
import UIKit
/// RadioButtonControllerDelegate. Delegate optionally implements didSelectButton that receives selected button.
@objc protocol NMPLRadioButtonControllerDelegate {
/**
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 NMPLRadioButtonsController : NSObject
{
private var buttonsArray = [UIButton]()
private weak var currentSelectedButton:UIButton? = nil
weak var delegate : NMPLRadioButtonControllerDelegate? = 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: "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: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
}
/**
Remove a UIButton from controller.
- parameter button: Button to be removed from controller.
*/
func removeButton(aButton: UIButton) {
var iteration = 0
var iteratingButton: UIButton? = nil
for( ; iteration<buttonsArray.count; iteration++) {
iteratingButton = buttonsArray[iteration]
if(iteratingButton == aButton) {
break
} else {
iteratingButton = nil
}
}
if(iteratingButton != nil) {
buttonsArray.removeAtIndex(iteration)
iteratingButton!.removeTarget(self, action: "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: "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
}
}
import UIKit
/// RadioButtonControllerDelegate. Delegate optionally implements didSelectButton that receives selected button.
@objc protocol NMPLRadioButtonControllerDelegate {
/**
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 NMPLRadioButtonsController : NSObject
{
private var buttonsArray = [UIButton]()
private weak var currentSelectedButton:UIButton? = nil
weak var delegate : NMPLRadioButtonControllerDelegate? = 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: "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: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
}
/**
Remove a UIButton from controller.
- parameter button: Button to be removed from controller.
*/
func removeButton(aButton: UIButton) {
var iteration = 0
var iteratingButton: UIButton? = nil
for( ; iteration<buttonsArray.count; iteration++) {
iteratingButton = buttonsArray[iteration]
if(iteratingButton == aButton) {
break
} else {
iteratingButton = nil
}
}
if(iteratingButton != nil) {
buttonsArray.removeAtIndex(iteration)
iteratingButton!.removeTarget(self, action: "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: "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.In storyboard
- Drag and drop 3 buttons,
- Select button1,button2,button3 create outlets for them in ViewController.
- Select button1,got to identity inspector and select the custom class NMPLRadioButton
- Below define an userdefined runtime attributes,click on + button below left,set keypath as circleColor,type as Color and value as color u want
- Next,click on + button below left,set keypath as circleRadius,type as Number and value as ex..,10,20 u want.
- Repeate,steps 3,4,5 for button2,button3.
4.Copy the Code Snippet in ViewController.swift.
ViewController.swift
import UIKit
class ViewController: UIViewController, NMPLRadioButtonControllerDelegate {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
var radioButtonController: NMPLRadioButtonsController?
override func viewDidLoad() {
super.viewDidLoad()
radioButtonController = NMPLRadioButtonsController(buttons: button1, button2, button3)
radioButtonController!.delegate = self
radioButtonController!.shouldLetDeSelect = true
// Do any additional setup after loading the view, typically from a nib.
}
func didSelectButton(aButton: UIButton?) {
print(aButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class ViewController: UIViewController, NMPLRadioButtonControllerDelegate {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
var radioButtonController: NMPLRadioButtonsController?
override func viewDidLoad() {
super.viewDidLoad()
radioButtonController = NMPLRadioButtonsController(buttons: button1, button2, button3)
radioButtonController!.delegate = self
radioButtonController!.shouldLetDeSelect = true
// Do any additional setup after loading the view, typically from a nib.
}
func didSelectButton(aButton: UIButton?) {
print(aButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
No comments:
Post a Comment