Commit b8b1b899 by Orkhan Alikhanov

Added documentation for Radio/Check things

parent edfe8046
......@@ -6,7 +6,11 @@
// Copyright © 2017 CosmicMind, Inc. All rights reserved.
//
import UIKit
open class BaseButtonGroup<T: Button>: View {
/// Holds reference to buttons within the group.
open var buttons: [T] = [] {
didSet {
oldValue.forEach {
......@@ -19,6 +23,9 @@ open class BaseButtonGroup<T: Button>: View {
}
}
/// Initializes group with the provided buttons.
///
/// - Parameter buttons: Array of buttons.
public convenience init(buttons: [T]) {
self.init(frame: .zero)
defer { self.buttons = buttons } // defer allows didSet to be called
......
......@@ -14,18 +14,28 @@ open class BaseIconLayerButton: Button {
class var iconLayer: BaseIconLayer { fatalError("Has to be implemented by subclasses") }
lazy var iconLayer: BaseIconLayer = { return type(of: self).iconLayer }()
/// A Boolean value indicating whether the button is in the selected state
///
/// Use `setSelected(_:, animated:)` if the state change needs to be animated
open override var isSelected: Bool {
didSet {
iconLayer.setSelected(isSelected, animated: false)
}
}
/// A Boolean value indicating whether the control is enabled.
open override var isEnabled: Bool {
didSet {
iconLayer.isEnabled = isEnabled
}
}
/// Sets the color of the icon to use for the specified state.
///
/// - Parameters:
/// - color: The color of the icon to use for the specified state.
/// - state: The state that uses the specified color. Supports only (.normal, .selected, .disabled)
open func setIconColor(_ color: UIColor, for state: UIControlState) {
switch state {
case .normal:
......@@ -38,7 +48,11 @@ open class BaseIconLayerButton: Button {
fatalError("unsupported state")
}
}
/// Returns the icon color used for a state.
///
/// - Parameter state: The state that uses the icon color. Supports only (.normal, .selected, .disabled)
/// - Returns: The color of the title for the specified state.
open func iconColor(for state: UIControlState) -> UIColor {
switch state {
case .normal:
......@@ -52,7 +66,15 @@ open class BaseIconLayerButton: Button {
}
}
/// A Boolean value indicating whether the button is being animated
open var isAnimating: Bool { return iconLayer.isAnimating }
/// Sets the `selected` state of the button, optionally animating the transition.
///
/// - Parameters:
/// - isSelected: A Boolean value indicating new `selected` state
/// - animated: true if the state change should be animated, otherwise false.
open func setSelected(_ isSelected: Bool, animated: Bool) {
guard !isAnimating else { return }
iconLayer.setSelected(isSelected, animated: animated)
......@@ -99,7 +121,7 @@ open class BaseIconLayerButton: Button {
visualLayer.frame.center = iconLayer.frame.center
visualLayer.cornerRadius = s / 2
}
private let margin: CGFloat = 5
private let iconSize: CGFloat = 16
}
......
......@@ -11,6 +11,7 @@ import UIKit
open class CheckButton: BaseIconLayerButton {
class override var iconLayer: BaseIconLayer { return CheckBoxLayer() }
/// Color of the checkmark (✓)
open var checkmarkColor: UIColor {
get {
return (iconLayer as! CheckBoxLayer).checkmarkColor
......
......@@ -6,16 +6,32 @@
// Copyright © 2017 CosmicMind, Inc. All rights reserved.
//
/// Lays out provided check buttons within itself.
///
/// Unlike RadioButtonGroup, checking one check button that belongs to a check group *does not* unchecks any previously checked
/// check button within the same group. Intially, all of the check buttons are unchecked.
///
/// The buttons are layout out by `Grid` system, so that changing properites of grid instance
/// (e.g interimSpace) are reflected.
open class CheckButtonGroup: BaseButtonGroup<CheckButton> {
/// Initializes CheckButtonGroup with an array of check buttons each having
/// title equal to corresponding string in the `titles` parameter.
///
/// - Parameter titles: An array of title strings
public convenience init(titles: [String]) {
let buttons = titles.map { CheckButton(title: $0) }
self.init(buttons: buttons)
}
/// Returns all selected check buttons within the group
/// or empty array if none is seleceted.
open var selecetedButtons: [CheckButton] {
return buttons.filter { $0.isSelected }
}
/// Returns indexes of all selected check buttons within the group
/// or empty array if none is seleceted.
open var selectedIndices: [Int] {
return selecetedButtons.map { buttons.index(of: $0)! }
}
......
......@@ -6,16 +6,33 @@
// Copyright © 2017 CosmicMind, Inc. All rights reserved.
//
/// Lays out provided radio buttons within itself.
///
/// Checking one radio button that belongs to a radio group unchecks any previously checked
/// radio button within the same group. Intially, all of the radio buttons are unchecked.
///
/// The buttons are layout out by `Grid` system, so that changing properites of grid instance
/// (e.g interimSpace) are reflected.
open class RadioButtonGroup: BaseButtonGroup<RadioButton> {
/// Initializes RadioButtonGroup with an array of radio buttons each having
/// title equal to corresponding string in the `titles` parameter.
///
/// - Parameter titles: An array of title strings.
public convenience init(titles: [String]) {
let buttons = titles.map { RadioButton(title: $0) }
self.init(buttons: buttons)
}
/// Returns selected radio button within the group.
/// If none is selected (e.g in initial state), nil is returned.
open var selectedButton: RadioButton? {
return buttons.first { $0.isSelected }
}
/// Returns index of selected radio button within the group.
/// If none is selected (e.g in initial state), -1 is returned.
open var selectedIndex: Int {
guard let b = selectedButton else { return -1 }
return buttons.index(of: b)!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment