Commit 09cdb27c by Daniel Dahan

development: updated SwitchControl to Switch

parent 412c2ca7
...@@ -31,642 +31,603 @@ ...@@ -31,642 +31,603 @@
import UIKit import UIKit
public class Layout { public class Layout {
/// Parent UIView context. /// Parent UIView context.
internal weak var parent: UIView? internal weak var parent: UIView?
/// Child UIView context. /// Child UIView context.
internal weak var child: UIView? internal weak var child: UIView?
/** /**
An initializer that takes in a parent context. An initializer that takes in a parent context.
- Parameter parent: An optional parent UIView. - Parameter parent: An optional parent UIView.
*/ */
public init(parent: UIView?) { public init(parent: UIView?) {
self.parent = parent self.parent = parent
} }
/** /**
An initializer that takes in a parent context and child context. An initializer that takes in a parent context and child context.
- Parameter parent: An optional parent UIView. - Parameter parent: An optional parent UIView.
- Parameter child: An optional child UIView. - Parameter child: An optional child UIView.
*/ */
public init(parent: UIView?, child: UIView?) { public init(parent: UIView?, child: UIView?) {
self.parent = parent self.parent = parent
self.child = child self.child = child
} }
/** /**
Prints a debug message when the parent context is not available. Prints a debug message when the parent context is not available.
- Parameter function: A String representation of the function that - Parameter function: A String representation of the function that
caused the issue. caused the issue.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
internal func debugParentNotAvailableMessage(function: String = #function) -> Layout { internal func debugParentNotAvailableMessage(function: String = #function) -> Layout {
debugPrint("[Material Layout Error: Parent view context is not available for \(function).") debugPrint("[Material Layout Error: Parent view context is not available for \(function).")
return self return self
} }
/** /**
Prints a debug message when the child context is not available. Prints a debug message when the child context is not available.
- Parameter function: A String representation of the function that - Parameter function: A String representation of the function that
caused the issue. caused the issue.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
internal func debugChildNotAvailableMessage(function: String = #function) -> Layout { internal func debugChildNotAvailableMessage(function: String = #function) -> Layout {
debugPrint("[Material Layout Error: Child view context is not available for \(function).") debugPrint("[Material Layout Error: Child view context is not available for \(function).")
return self return self
} }
/** /**
Sets the width of a view. Sets the width of a view.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter width: A CGFloat value. - Parameter width: A CGFloat value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func width(_ child: UIView, width: CGFloat, multiplier: CGFloat = 1) -> Layout { public func width(_ child: UIView, width: CGFloat) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.width(parent: v, child: child, width: width, multiplier: multiplier) Layout.width(parent: v, child: child, width: width)
return self return self
} }
/** /**
Sets the width of a view assuming a child context view. Sets the width of a view assuming a child context view.
- Parameter width: A CGFloat value. - Parameter width: A CGFloat value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func width(_ width: CGFloat, multiplier: CGFloat = 1) -> Layout { public func width(_ width: CGFloat) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.width(v, width: width, multiplier: multiplier) return self.width(v, width: width)
} }
/** /**
Sets the height of a view. Sets the height of a view.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter height: A CGFloat value. - Parameter height: A CGFloat value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func height(_ child: UIView, height: CGFloat, multiplier: CGFloat = 1) -> Layout { public func height(_ child: UIView, height: CGFloat) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.height(parent: v, child: child, height: height, multiplier: multiplier) Layout.height(parent: v, child: child, height: height)
return self return self
} }
/** /**
Sets the height of a view assuming a child context view. Sets the height of a view assuming a child context view.
- Parameter height: A CGFloat value. - Parameter height: A CGFloat value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func height(_ height: CGFloat, multiplier: CGFloat = 1) -> Layout { public func height(_ height: CGFloat) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.height(v, height: height, multiplier: multiplier) return self.height(v, height: height)
} }
/** /**
Sets the width and height of a view. Sets the width and height of a view.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter size: A CGSize value. - Parameter size: A CGSize value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func size(_ child: UIView, size: CGSize, multiplier: CGFloat = 1) -> Layout { public func size(_ child: UIView, size: CGSize) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.size(parent: v, child: child, size: size, multiplier: multiplier) Layout.size(parent: v, child: child, size: size)
return self return self
} }
/** /**
Sets the width and height of a view assuming a child context view. Sets the width and height of a view assuming a child context view.
- Parameter size: A CGSize value. - Parameter size: A CGSize value.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func size(_ size: CGSize = CGSize.zero, multiplier: CGFloat = 1) -> Layout { public func size(_ size: CGSize = CGSize.zero) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.size(v, size: size, multiplier: multiplier) return self.size(v, size: size)
} }
/** /**
A collection of children views are horizontally stretched with optional left, A collection of children views are horizontally stretched with optional left,
right padding and interim interimSpace. right padding and interim interimSpace.
- Parameter children: An Array UIView to layout. - Parameter children: An Array UIView to layout.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter interimSpace: A CGFloat value for interim interimSpace. - Parameter interimSpace: A CGFloat value for interim interimSpace.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func horizontally(_ children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0, multiplier: CGFloat = 1) -> Layout { public func horizontally(_ children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
Layout.horizontally(parent: v, children: children, left: left, right: right, interimSpace: interimSpace, multiplier: multiplier) Layout.horizontally(parent: v, children: children, left: left, right: right, interimSpace: interimSpace)
return self return self
} }
/** /**
A collection of children views are vertically stretched with optional top, A collection of children views are vertically stretched with optional top,
bottom padding and interim interimSpace. bottom padding and interim interimSpace.
- Parameter children: An Array UIView to layout. - Parameter children: An Array UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter interimSpace: A CGFloat value for interim interimSpace. - Parameter interimSpace: A CGFloat value for interim interimSpace.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func vertically(_ children: [UIView], top: CGFloat = 0, bottom: CGFloat = 0, interimSpace: InterimSpace = 0, multiplier: CGFloat = 1) -> Layout { public func vertically(_ children: [UIView], top: CGFloat = 0, bottom: CGFloat = 0, interimSpace: InterimSpace = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
Layout.vertically(parent: v, children: children, top: top, bottom: bottom, interimSpace: interimSpace, multiplier: multiplier) Layout.vertically(parent: v, children: children, top: top, bottom: bottom, interimSpace: interimSpace)
return self return self
} }
/** /**
A child view is horizontally stretched with optional left and right padding. A child view is horizontally stretched with optional left and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func horizontally(_ child: UIView, left: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func horizontally(_ child: UIView, left: CGFloat = 0, right: CGFloat = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.horizontally(parent: v, child: child, left: left, right: right, multiplier: multiplier) Layout.horizontally(parent: v, child: child, left: left, right: right)
return self return self
} }
/** /**
A child view is horizontally stretched with optional left and right padding. A child view is horizontally stretched with optional left and right padding.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func horizontally(left: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func horizontally(left: CGFloat = 0, right: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return horizontally(v, left: left, right: right, multiplier: multiplier) return horizontally(v, left: left, right: right)
} }
/** /**
A child view is vertically stretched with optional left and right padding. A child view is vertically stretched with optional left and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func vertically(_ child: UIView, top: CGFloat = 0, bottom: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func vertically(_ child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.vertically(parent: v, child: child, top: top, bottom: bottom, multiplier: multiplier) Layout.vertically(parent: v, child: child, top: top, bottom: bottom)
return self return self
} }
/** /**
A child view is vertically stretched with optional left and right padding. A child view is vertically stretched with optional left and right padding.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func vertically(top: CGFloat = 0, bottom: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func vertically(top: CGFloat = 0, bottom: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return vertically(v, top: top, bottom: bottom, multiplier: multiplier) return vertically(v, top: top, bottom: bottom)
} }
/** /**
A child view is vertically and horizontally stretched with optional top, left, bottom and right padding. A child view is vertically and horizontally stretched with optional top, left, bottom and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func edges(_ child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
public func edges(_ child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.edges(parent: v, child: child, top: top, left: left, bottom: bottom, right: right)
Layout.edges(parent: v, child: child, top: top, left: left, bottom: bottom, right: right, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is vertically and horizontally stretched with optional top, left, bottom and right padding. A child view is vertically and horizontally stretched with optional top, left, bottom and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func edges(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func edges(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return edges(v, top: top, left: left, bottom: bottom, right: right, multiplier: multiplier) return edges(v, top: top, left: left, bottom: bottom, right: right)
} }
/** /**
A child view is aligned from the top with optional top padding. A child view is aligned from the top with optional top padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func top(_ child: UIView, top: CGFloat = 0) -> Layout {
public func top(_ child: UIView, top: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.top(parent: v, child: child, top: top)
Layout.top(parent: v, child: child, top: top, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the top with optional top padding. A child view is aligned from the top with optional top padding.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func top(_ top: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func top(_ top: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.top(v, top: top, multiplier: multiplier) return self.top(v, top: top)
} }
/** /**
A child view is aligned from the left with optional left padding. A child view is aligned from the left with optional left padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func left(_ child: UIView, left: CGFloat = 0) -> Layout {
public func left(_ child: UIView, left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.left(parent: v, child: child, left: left)
Layout.left(parent: v, child: child, left: left, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the left with optional left padding. A child view is aligned from the left with optional left padding.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func left(_ left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func left(_ left: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.left(v, left: left, multiplier: multiplier) return self.left(v, left: left)
} }
/** /**
A child view is aligned from the bottom with optional bottom padding. A child view is aligned from the bottom with optional bottom padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func bottom(_ child: UIView, bottom: CGFloat = 0) -> Layout {
public func bottom(_ child: UIView, bottom: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.bottom(parent: v, child: child, bottom: bottom)
Layout.bottom(parent: v, child: child, bottom: bottom, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the bottom with optional bottom padding. A child view is aligned from the bottom with optional bottom padding.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func bottom(_ bottom: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func bottom(_ bottom: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.bottom(v, bottom: bottom, multiplier: multiplier) return self.bottom(v, bottom: bottom)
} }
/** /**
A child view is aligned from the right with optional right padding. A child view is aligned from the right with optional right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func right(_ child: UIView, right: CGFloat = 0) -> Layout {
public func right(_ child: UIView, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.right(parent: v, child: child, right: right)
Layout.right(parent: v, child: child, right: right, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the right with optional right padding. A child view is aligned from the right with optional right padding.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func right(_ right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func right(_ right: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return self.right(v, right: right, multiplier: multiplier) return self.right(v, right: right)
} }
/** /**
A child view is aligned from the top left with optional top and left padding. A child view is aligned from the top left with optional top and left padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func topLeft(_ child: UIView, top: CGFloat = 0, left: CGFloat = 0) -> Layout {
public func topLeft(_ child: UIView, top: CGFloat = 0, left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.topLeft(parent: v, child: child, top: top, left: left)
Layout.topLeft(parent: v, child: child, top: top, left: left, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the top left with optional top and left padding. A child view is aligned from the top left with optional top and left padding.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func topLeft(top: CGFloat = 0, left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func topLeft(top: CGFloat = 0, left: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return topLeft(v, top: top, left: left, multiplier: multiplier) return topLeft(v, top: top, left: left)
} }
/** /**
A child view is aligned from the top right with optional top and right padding. A child view is aligned from the top right with optional top and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func topRight(_ child: UIView, top: CGFloat = 0, right: CGFloat = 0) -> Layout {
public func topRight(_ child: UIView, top: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.topRight(parent: v, child: child, top: top, right: right)
Layout.topRight(parent: v, child: child, top: top, right: right, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the top right with optional top and right padding. A child view is aligned from the top right with optional top and right padding.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func topRight(top: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func topRight(top: CGFloat = 0, right: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return topRight(v, top: top, right: right, multiplier: multiplier) return topRight(v, top: top, right: right)
} }
/** /**
A child view is aligned from the bottom left with optional bottom and left padding. A child view is aligned from the bottom left with optional bottom and left padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func bottomLeft(_ child: UIView, bottom: CGFloat = 0, left: CGFloat = 0) -> Layout {
public func bottomLeft(_ child: UIView, bottom: CGFloat = 0, left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.bottomLeft(parent: v, child: child, bottom: bottom, left: left)
Layout.bottomLeft(parent: v, child: child, bottom: bottom, left: left, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the bottom left with optional bottom and left padding. A child view is aligned from the bottom left with optional bottom and left padding.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func bottomLeft(bottom: CGFloat = 0, left: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func bottomLeft(bottom: CGFloat = 0, left: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return bottomLeft(v, bottom: bottom, left: left, multiplier: multiplier) return bottomLeft(v, bottom: bottom, left: left)
} }
/** /**
A child view is aligned from the bottom right with optional bottom and right padding. A child view is aligned from the bottom right with optional bottom and right padding.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. - Returns: The current Layout instance.
- Returns: The current Layout instance. */
*/ public func bottomRight(_ child: UIView, bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
public func bottomRight(_ child: UIView, bottom: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { guard let v = parent else {
guard let v = parent else { return debugParentNotAvailableMessage()
return debugParentNotAvailableMessage() }
} self.child = child
self.child = child Layout.bottomRight(parent: v, child: child, bottom: bottom, right: right)
Layout.bottomRight(parent: v, child: child, bottom: bottom, right: right, multiplier: multiplier) return self
return self }
}
/**
/**
A child view is aligned from the bottom right with optional bottom and right padding. A child view is aligned from the bottom right with optional bottom and right padding.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func bottomRight(bottom: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func bottomRight(bottom: CGFloat = 0, right: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return bottomRight(v, bottom: bottom, right: right, multiplier: multiplier) return bottomRight(v, bottom: bottom, right: right)
} }
/** /**
A child view is aligned at the center with an optional offsetX and offsetY value. A child view is aligned at the center with an optional offsetX and offsetY value.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offsetX: A CGFloat value for the offset along the x axis. - Parameter offsetX: A CGFloat value for the offset along the x axis.
- Parameter offsetX: A CGFloat value for the offset along the y axis. - Parameter offsetX: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func center(_ child: UIView, offsetX: CGFloat = 0, offsetY: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func center(_ child: UIView, offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.center(parent: v, child: child, offsetX: offsetX, offsetY: offsetY, multiplier: multiplier) Layout.center(parent: v, child: child, offsetX: offsetX, offsetY: offsetY)
return self return self
} }
/** /**
A child view is aligned at the center with an optional offsetX and offsetY value. A child view is aligned at the center with an optional offsetX and offsetY value.
- Parameter offsetX: A CGFloat value for the offset along the x axis. - Parameter offsetX: A CGFloat value for the offset along the x axis.
- Parameter offsetX: A CGFloat value for the offset along the y axis. - Parameter offsetX: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func center(offsetX: CGFloat = 0, offsetY: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func center(offsetX: CGFloat = 0, offsetY: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return center(v, offsetX: offsetX, offsetY: offsetY, multiplier: multiplier) return center(v, offsetX: offsetX, offsetY: offsetY)
} }
/** /**
A child view is aligned at the center horizontally with an optional offset value. A child view is aligned at the center horizontally with an optional offset value.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offset: A CGFloat value for the offset along the x axis. - Parameter offset: A CGFloat value for the offset along the x axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func centerHorizontally(_ child: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func centerHorizontally(_ child: UIView, offset: CGFloat = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.centerHorizontally(parent: v, child: child, offset: offset, multiplier: multiplier) Layout.centerHorizontally(parent: v, child: child, offset: offset)
return self return self
} }
/** /**
A child view is aligned at the center horizontally with an optional offset value. A child view is aligned at the center horizontally with an optional offset value.
- Parameter offset: A CGFloat value for the offset along the x axis. - Parameter offset: A CGFloat value for the offset along the x axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func centerHorizontally(offset: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func centerHorizontally(offset: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return centerHorizontally(v, offset: offset, multiplier: multiplier) return centerHorizontally(v, offset: offset)
} }
/** /**
A child view is aligned at the center vertically with an optional offset value. A child view is aligned at the center vertically with an optional offset value.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offset: A CGFloat value for the offset along the y axis. - Parameter offset: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func centerVertically(_ child: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func centerVertically(_ child: UIView, offset: CGFloat = 0) -> Layout {
guard let v = parent else { guard let v = parent else {
return debugParentNotAvailableMessage() return debugParentNotAvailableMessage()
} }
self.child = child self.child = child
Layout.centerVertically(parent: v, child: child, offset: offset, multiplier: multiplier) Layout.centerVertically(parent: v, child: child, offset: offset)
return self return self
} }
/** /**
A child view is aligned at the center vertically with an optional offset value. A child view is aligned at the center vertically with an optional offset value.
- Parameter offset: A CGFloat value for the offset along the y axis. - Parameter offset: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public func centerVertically(offset: CGFloat = 0, multiplier: CGFloat = 1) -> Layout { public func centerVertically(offset: CGFloat = 0) -> Layout {
guard let v = child else { guard let v = child else {
return debugChildNotAvailableMessage() return debugChildNotAvailableMessage()
} }
return centerVertically(v, offset: offset, multiplier: multiplier) return centerVertically(v, offset: offset)
} }
} }
/// Layout /// Layout
extension Layout { extension Layout {
/** /**
Sets the width of a view. Sets the width of a view.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter multiplier: A CGFloat value.
- Parameter width: A CGFloat value. - Parameter width: A CGFloat value.
*/ */
public class func width(parent: UIView, child: UIView, width: CGFloat = 0, multiplier: CGFloat = 1) { public class func width(parent: UIView, child: UIView, width: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: multiplier, constant: width)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: width))
} }
/** /**
Sets the height of a view. Sets the height of a view.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter multiplier: A CGFloat value.
- Parameter height: A CGFloat value. - Parameter height: A CGFloat value.
*/ */
public class func height(parent: UIView, child: UIView, height: CGFloat = 0, multiplier: CGFloat = 1) { public class func height(parent: UIView, child: UIView, height: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: multiplier, constant: height)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height))
} }
/** /**
Sets the width and height of a view. Sets the width and height of a view.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter multiplier: A CGFloat value.
- Parameter size: A CGSize value. - Parameter size: A CGSize value.
*/ */
public class func size(parent: UIView, child: UIView, size: CGSize = CGSize.zero, multiplier: CGFloat = 1) { public class func size(parent: UIView, child: UIView, size: CGSize = CGSize.zero) {
Layout.width(parent: parent, child: child, width: size.width, multiplier: multiplier) Layout.width(parent: parent, child: child, width: size.width)
Layout.height(parent: parent, child: child, height: size.height, multiplier: multiplier) Layout.height(parent: parent, child: child, height: size.height)
} }
/** /**
A collection of children views are horizontally stretched with optional left, A collection of children views are horizontally stretched with optional left,
right padding and interim interimSpace. right padding and interim interimSpace.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
...@@ -674,23 +635,20 @@ extension Layout { ...@@ -674,23 +635,20 @@ extension Layout {
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter interimSpace: A CGFloat value for interim interimSpace. - Parameter interimSpace: A CGFloat value for interim interimSpace.
- Parameter multiplier: A CGFloat value.
*/ */
public class func horizontally(parent: UIView, children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0, multiplier: CGFloat = 1) { public class func horizontally(parent: UIView, children: [UIView], left: CGFloat = 0, right: CGFloat = 0, interimSpace: InterimSpace = 0) {
guard 0 < children.count else {
return
}
prepareForConstraint(parent, children: children) prepareForConstraint(parent, children: children)
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: multiplier, constant: left)) if 0 < children.count {
for i in 1..<children.count { parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .left, relatedBy: .equal, toItem: children[i - 1], attribute: .right, multiplier: multiplier, constant: interimSpace)) for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .width, relatedBy: .equal, toItem: children[0], attribute: .width, multiplier: multiplier, constant: 0)) parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .left, relatedBy: .equal, toItem: children[i - 1], attribute: .right, multiplier: 1, constant: interimSpace))
} parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .width, relatedBy: .equal, toItem: children[0], attribute: .width, multiplier: 1, constant: 0))
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: multiplier, constant: -right)) }
} parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
}
/** }
/**
A collection of children views are vertically stretched with optional top, A collection of children views are vertically stretched with optional top,
bottom padding and interim interimSpace. bottom padding and interim interimSpace.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
...@@ -698,51 +656,46 @@ extension Layout { ...@@ -698,51 +656,46 @@ extension Layout {
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter interimSpace: A CGFloat value for interim interimSpace. - Parameter interimSpace: A CGFloat value for interim interimSpace.
- Parameter multiplier: A CGFloat value.
*/ */
public class func vertically(parent: UIView, children: [UIView], top: CGFloat = 0, bottom: CGFloat = 0, interimSpace: InterimSpace = 0, multiplier: CGFloat = 1) { public class func vertically(parent: UIView, children: [UIView], top: CGFloat = 0, bottom: CGFloat = 0, interimSpace: InterimSpace = 0) {
guard 0 < children.count else {
return
}
prepareForConstraint(parent, children: children) prepareForConstraint(parent, children: children)
if 0 < children.count {
parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: multiplier, constant: top)) parent.addConstraint(NSLayoutConstraint(item: children[0], attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
for i in 1..<children.count { for i in 1..<children.count {
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .top, relatedBy: .equal, toItem: children[i - 1], attribute: .bottom, multiplier: multiplier, constant: interimSpace)) parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .top, relatedBy: .equal, toItem: children[i - 1], attribute: .bottom, multiplier: 1, constant: interimSpace))
parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .height, relatedBy: .equal, toItem: children[0], attribute: .height, multiplier: multiplier, constant: 0)) parent.addConstraint(NSLayoutConstraint(item: children[i], attribute: .height, relatedBy: .equal, toItem: children[0], attribute: .height, multiplier: 1, constant: 0))
} }
parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: multiplier, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: children[children.count - 1], attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
} }
}
/**
/**
A child view is horizontally stretched with optional left and right padding. A child view is horizontally stretched with optional left and right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. */
*/ public class func horizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0) {
public class func horizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) { prepareForConstraint(parent, child: child)
prepareForConstraint(parent, child: child) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: multiplier, constant: left)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: multiplier, constant: -right)) }
}
/**
/**
A child view is vertically stretched with optional left and right padding. A child view is vertically stretched with optional left and right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value. */
*/ public class func vertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0) {
public class func vertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0, multiplier: CGFloat = 1) { prepareForConstraint(parent, child: child)
prepareForConstraint(parent, child: child) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: multiplier, constant: top)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: multiplier, constant: -bottom)) }
}
/**
/**
A child view is vertically and horizontally stretched with optional top, left, bottom and right padding. A child view is vertically and horizontally stretched with optional top, left, bottom and right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
...@@ -750,162 +703,150 @@ extension Layout { ...@@ -750,162 +703,150 @@ extension Layout {
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value. */
*/ public class func edges(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0) {
public class func edges(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0, multiplier: CGFloat = 1) { horizontally(parent: parent, child: child, left: left, right: right)
horizontally(parent: parent, child: child, left: left, right: right, multiplier: multiplier) vertically(parent: parent, child: child, top: top, bottom: bottom)
vertically(parent: parent, child: child, top: top, bottom: bottom, multiplier: multiplier) }
}
/**
/**
A child view is aligned from the top with optional top padding. A child view is aligned from the top with optional top padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func top(parent: UIView, child: UIView, top: CGFloat = 0, multiplier: CGFloat = 1) { public class func top(parent: UIView, child: UIView, top: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: multiplier, constant: top)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1, constant: top))
} }
/** /**
A child view is aligned from the left with optional left padding. A child view is aligned from the left with optional left padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func left(parent: UIView, child: UIView, left: CGFloat = 0, multiplier: CGFloat = 1) { public class func left(parent: UIView, child: UIView, left: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: multiplier, constant: left)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1, constant: left))
} }
/** /**
A child view is aligned from the bottom with optional bottom padding. A child view is aligned from the bottom with optional bottom padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0, multiplier: CGFloat = 1) { public class func bottom(parent: UIView, child: UIView, bottom: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: multiplier, constant: -bottom)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1, constant: -bottom))
} }
/** /**
A child view is aligned from the right with optional right padding. A child view is aligned from the right with optional right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func right(parent: UIView, child: UIView, right: CGFloat = 0, multiplier: CGFloat = 1) { public class func right(parent: UIView, child: UIView, right: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: multiplier, constant: -right)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1, constant: -right))
} }
/** /**
A child view is aligned from the top left with optional top and left padding. A child view is aligned from the top left with optional top and left padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func topLeft(parent: UIView, child: UIView, top t: CGFloat = 0, left l: CGFloat = 0, multiplier: CGFloat = 1) { public class func topLeft(parent: UIView, child: UIView, top t: CGFloat = 0, left l: CGFloat = 0) {
top(parent: parent, child: child, top: t, multiplier: multiplier) top(parent: parent, child: child, top: t)
left(parent: parent, child: child, left: l, multiplier: multiplier) left(parent: parent, child: child, left: l)
} }
/** /**
A child view is aligned from the top right with optional top and right padding. A child view is aligned from the top right with optional top and right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter top: A CGFloat value for padding the top side. - Parameter top: A CGFloat value for padding the top side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func topRight(parent: UIView, child: UIView, top t: CGFloat = 0, right r: CGFloat = 0, multiplier: CGFloat = 1) { public class func topRight(parent: UIView, child: UIView, top t: CGFloat = 0, right r: CGFloat = 0) {
top(parent: parent, child: child, top: t, multiplier: multiplier) top(parent: parent, child: child, top: t)
right(parent: parent, child: child, right: r, multiplier: multiplier) right(parent: parent, child: child, right: r)
} }
/** /**
A child view is aligned from the bottom left with optional bottom and left padding. A child view is aligned from the bottom left with optional bottom and left padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter left: A CGFloat value for padding the left side. - Parameter left: A CGFloat value for padding the left side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func bottomLeft(parent: UIView, child: UIView, bottom b: CGFloat = 0, left l: CGFloat = 0, multiplier: CGFloat = 1) { public class func bottomLeft(parent: UIView, child: UIView, bottom b: CGFloat = 0, left l: CGFloat = 0) {
bottom(parent: parent, child: child, bottom: b, multiplier: multiplier) bottom(parent: parent, child: child, bottom: b)
left(parent: parent, child: child, left: l, multiplier: multiplier) left(parent: parent, child: child, left: l)
} }
/** /**
A child view is aligned from the bottom right with optional bottom and right padding. A child view is aligned from the bottom right with optional bottom and right padding.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter bottom: A CGFloat value for padding the bottom side. - Parameter bottom: A CGFloat value for padding the bottom side.
- Parameter right: A CGFloat value for padding the right side. - Parameter right: A CGFloat value for padding the right side.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func bottomRight(parent: UIView, child: UIView, bottom b: CGFloat = 0, right r: CGFloat = 0, multiplier: CGFloat = 1) { public class func bottomRight(parent: UIView, child: UIView, bottom b: CGFloat = 0, right r: CGFloat = 0) {
bottom(parent: parent, child: child, bottom: b, multiplier: multiplier) bottom(parent: parent, child: child, bottom: b)
right(parent: parent, child: child, right: r, multiplier: multiplier) right(parent: parent, child: child, right: r)
} }
/** /**
A child view is aligned at the center with an optional offsetX and offsetY value. A child view is aligned at the center with an optional offsetX and offsetY value.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offsetX: A CGFloat value for the offset along the x axis. - Parameter offsetX: A CGFloat value for the offset along the x axis.
- Parameter offsetX: A CGFloat value for the offset along the y axis. - Parameter offsetX: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func center(parent: UIView, child: UIView, offsetX: CGFloat = 0, offsetY: CGFloat = 0, multiplier: CGFloat = 1) { public class func center(parent: UIView, child: UIView, offsetX: CGFloat = 0, offsetY: CGFloat = 0) {
centerHorizontally(parent: parent, child: child, offset: offsetX, multiplier: multiplier) centerHorizontally(parent: parent, child: child, offset: offsetX)
centerVertically(parent: parent, child: child, offset: offsetY, multiplier: multiplier) centerVertically(parent: parent, child: child, offset: offsetY)
} }
/** /**
A child view is aligned at the center horizontally with an optional offset value. A child view is aligned at the center horizontally with an optional offset value.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offset: A CGFloat value for the offset along the y axis. - Parameter offset: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1) { public class func centerHorizontally(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: multiplier, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: offset))
} }
/** /**
A child view is aligned at the center vertically with an optional offset value. A child view is aligned at the center vertically with an optional offset value.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Parameter offset: A CGFloat value for the offset along the y axis. - Parameter offset: A CGFloat value for the offset along the y axis.
- Parameter multiplier: A CGFloat value.
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0, multiplier: CGFloat = 1) { public class func centerVertically(parent: UIView, child: UIView, offset: CGFloat = 0) {
prepareForConstraint(parent, child: child) prepareForConstraint(parent, child: child)
parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: multiplier, constant: offset)) parent.addConstraint(NSLayoutConstraint(item: child, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: offset))
} }
/** /**
Creats an Array with a NSLayoutConstraint value. Creats an Array with a NSLayoutConstraint value.
- Parameter format: The VFL format string. - Parameter format: The VFL format string.
- Parameter options: Additional NSLayoutFormatOptions. - Parameter options: Additional NSLayoutFormatOptions.
...@@ -914,45 +855,45 @@ extension Layout { ...@@ -914,45 +855,45 @@ extension Layout {
- Returns: The Array<NSLayoutConstraint> instance. - Returns: The Array<NSLayoutConstraint> instance.
*/ */
public class func constraint(format: String, options: NSLayoutFormatOptions, metrics: [String: Any]?, views: [String: Any]) -> [NSLayoutConstraint] { public class func constraint(format: String, options: NSLayoutFormatOptions, metrics: [String: Any]?, views: [String: Any]) -> [NSLayoutConstraint] {
for (_, a) in views { for (_, a) in views {
if let v = a as? UIView { if let v = a as? UIView {
v.translatesAutoresizingMaskIntoConstraints = false v.translatesAutoresizingMaskIntoConstraints = false
} }
} }
return NSLayoutConstraint.constraints( return NSLayoutConstraint.constraints(
withVisualFormat: format, withVisualFormat: format,
options: options, options: options,
metrics: metrics, metrics: metrics,
views: views views: views
) )
} }
/** /**
Prepares the relationship between the parent view context and child view Prepares the relationship between the parent view context and child view
to layout. If the child is not already added to the view hierarchy as the to layout. If the child is not already added to the view hierarchy as the
parent's child, then it is added. parent's child, then it is added.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
*/ */
private class func prepareForConstraint(_ parent: UIView, child: UIView) { private class func prepareForConstraint(_ parent: UIView, child: UIView) {
if parent != child.superview { if parent != child.superview {
child.removeFromSuperview() child.removeFromSuperview()
parent.addSubview(child) parent.addSubview(child)
} }
child.translatesAutoresizingMaskIntoConstraints = false child.translatesAutoresizingMaskIntoConstraints = false
} }
/** /**
Prepares the relationship between the parent view context and an Array of Prepares the relationship between the parent view context and an Array of
child UIViews. child UIViews.
- Parameter parent: A parent UIView context. - Parameter parent: A parent UIView context.
- Parameter children: An Array of UIViews. - Parameter children: An Array of UIViews.
*/ */
private class func prepareForConstraint(_ parent: UIView, children: [UIView]) { private class func prepareForConstraint(_ parent: UIView, children: [UIView]) {
for v in children { for v in children {
prepareForConstraint(parent, child: v) prepareForConstraint(parent, child: v)
} }
} }
} }
/// A memory reference to the LayoutKey instance for UIView extensions. /// A memory reference to the LayoutKey instance for UIView extensions.
...@@ -960,19 +901,19 @@ private var LayoutKey: UInt8 = 0 ...@@ -960,19 +901,19 @@ private var LayoutKey: UInt8 = 0
/// Layout extension for UIView. /// Layout extension for UIView.
extension UIView { extension UIView {
/// Layout reference. /// Layout reference.
public private(set) var layout: Layout { public private(set) var layout: Layout {
get { get {
return AssociatedObject(base: self, key: &LayoutKey) { return AssociatedObject(base: self, key: &LayoutKey) {
return Layout(parent: self) return Layout(parent: self)
} }
} }
set(value) { set(value) {
AssociateObject(base: self, key: &LayoutKey, value: value) AssociateObject(base: self, key: &LayoutKey, value: value)
} }
} }
/** /**
Used to chain layout constraints on a child context. Used to chain layout constraints on a child context.
- Parameter child: A child UIView to layout. - Parameter child: A child UIView to layout.
- Returns: The current Layout instance. - Returns: The current Layout instance.
......
...@@ -30,38 +30,38 @@ ...@@ -30,38 +30,38 @@
import UIKit import UIKit
@objc(SwitchControlStyle) @objc(SwitchStyle)
public enum SwitchControlStyle: Int { public enum SwitchStyle: Int {
case light case light
case dark case dark
} }
@objc(SwitchControlState) @objc(SwitchState)
public enum SwitchControlState: Int { public enum SwitchState: Int {
case on case on
case off case off
} }
@objc(SwitchControlSize) @objc(SwitchSize)
public enum SwitchControlSize: Int { public enum SwitchSize: Int {
case small case small
case medium case medium
case large case large
} }
@objc(SwitchControlDelegate) @objc(SwitchDelegate)
public protocol SwitchControlDelegate { public protocol SwitchDelegate {
/** /**
A SwitchControl delegate method for state changes. A Switch delegate method for state changes.
- Parameter control: SwitchControl control. - Parameter control: Switch control.
*/ */
func switchStateChanged(control: SwitchControl) func switchStateChanged(control: Switch)
} }
@objc(SwitchControl) @objc(Switch)
open class SwitchControl: UIControl { open class Switch: UIControl {
/// An internal reference to the switchState public property. /// An internal reference to the switchState public property.
private var internalSwitchControlState: SwitchControlState = .off private var internalSwitchState: SwitchState = .off
/// Track thickness. /// Track thickness.
private var trackThickness: CGFloat = 0 private var trackThickness: CGFloat = 0
...@@ -79,7 +79,7 @@ open class SwitchControl: UIControl { ...@@ -79,7 +79,7 @@ open class SwitchControl: UIControl {
private var bounceOffset: CGFloat = 3 private var bounceOffset: CGFloat = 3
/// An Optional delegation method. /// An Optional delegation method.
open weak var delegate: SwitchControlDelegate? open weak var delegate: SwitchDelegate?
/// Indicates if the animation should bounce. /// Indicates if the animation should bounce.
@IBInspectable @IBInspectable
...@@ -170,7 +170,7 @@ open class SwitchControl: UIControl { ...@@ -170,7 +170,7 @@ open class SwitchControl: UIControl {
@IBInspectable @IBInspectable
open override var isEnabled: Bool { open override var isEnabled: Bool {
didSet { didSet {
styleForState(state: internalSwitchControlState) styleForState(state: internalSwitchState)
} }
} }
...@@ -178,27 +178,27 @@ open class SwitchControl: UIControl { ...@@ -178,27 +178,27 @@ open class SwitchControl: UIControl {
@IBInspectable @IBInspectable
public var on: Bool { public var on: Bool {
get { get {
return .on == internalSwitchControlState return .on == internalSwitchState
} }
set(value) { set(value) {
setOn(on: value, animated: true) setOn(on: value, animated: true)
} }
} }
/// SwitchControl state. /// Switch state.
public var switchState: SwitchControlState { public var switchState: SwitchState {
get { get {
return internalSwitchControlState return internalSwitchState
} }
set(value) { set(value) {
if value != internalSwitchControlState { if value != internalSwitchState {
internalSwitchControlState = value internalSwitchState = value
} }
} }
} }
/// SwitchControl style. /// Switch style.
public var switchStyle: SwitchControlStyle = .dark { public var switchStyle: SwitchStyle = .dark {
didSet { didSet {
switch switchStyle { switch switchStyle {
case .light: case .light:
...@@ -223,8 +223,8 @@ open class SwitchControl: UIControl { ...@@ -223,8 +223,8 @@ open class SwitchControl: UIControl {
} }
} }
/// SwitchControl size. /// Switch size.
public var switchSize: SwitchControlSize = .medium { public var switchSize: SwitchSize = .medium {
didSet { didSet {
switch switchSize { switch switchSize {
case .small: case .small:
...@@ -245,13 +245,13 @@ open class SwitchControl: UIControl { ...@@ -245,13 +245,13 @@ open class SwitchControl: UIControl {
open override var frame: CGRect { open override var frame: CGRect {
didSet { didSet {
layoutSwitchControl() layoutSwitch()
} }
} }
open override var bounds: CGRect { open override var bounds: CGRect {
didSet { didSet {
layoutSwitchControl() layoutSwitch()
} }
} }
...@@ -276,9 +276,9 @@ open class SwitchControl: UIControl { ...@@ -276,9 +276,9 @@ open class SwitchControl: UIControl {
super.init(coder: aDecoder) super.init(coder: aDecoder)
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchControlSize(size: .medium) prepareSwitchSize(size: .medium)
prepareSwitchControlStyle(style: .light) prepareSwitchStyle(style: .light)
prepareSwitchControlState(state: .off) prepareSwitchState(state: .off)
} }
/** /**
...@@ -294,39 +294,39 @@ open class SwitchControl: UIControl { ...@@ -294,39 +294,39 @@ open class SwitchControl: UIControl {
super.init(frame: frame) super.init(frame: frame)
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchControlSize(size: .medium) prepareSwitchSize(size: .medium)
prepareSwitchControlStyle(style: .light) prepareSwitchStyle(style: .light)
prepareSwitchControlState(state: .off) prepareSwitchState(state: .off)
} }
/** /**
An initializer that sets the state, style, and size of the SwitchControl instance. An initializer that sets the state, style, and size of the Switch instance.
- Parameter state: A SwitchControlState value. - Parameter state: A SwitchState value.
- Parameter style: A SwitchControlStyle value. - Parameter style: A SwitchStyle value.
- Parameter size: A SwitchControlSize value. - Parameter size: A SwitchSize value.
*/ */
public init(state: SwitchControlState = .off, style: SwitchControlStyle = .dark, size: SwitchControlSize = .medium) { public init(state: SwitchState = .off, style: SwitchStyle = .dark, size: SwitchSize = .medium) {
trackLayer = CAShapeLayer() trackLayer = CAShapeLayer()
button = FabButton() button = FabButton()
super.init(frame: CGRect.null) super.init(frame: CGRect.null)
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchControlSize(size: size) prepareSwitchSize(size: size)
prepareSwitchControlStyle(style: style) prepareSwitchStyle(style: style)
prepareSwitchControlState(state: state) prepareSwitchState(state: state)
} }
open override func willMove(toSuperview newSuperview: UIView?) { open override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview) super.willMove(toSuperview: newSuperview)
styleForState(state: internalSwitchControlState) styleForState(state: internalSwitchState)
} }
/** /**
Toggle the SwitchControl state, if On will be Off, and if Off will be On. Toggle the Switch state, if On will be Off, and if Off will be On.
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
public func toggle(completion: ((SwitchControl) -> Void)? = nil) { public func toggle(completion: ((Switch) -> Void)? = nil) {
setSwitchControlState(state: .on == internalSwitchControlState ? .off : .on, animated: true, completion: completion) setSwitchState(state: .on == internalSwitchState ? .off : .on, animated: true, completion: completion)
} }
/** /**
...@@ -334,22 +334,22 @@ open class SwitchControl: UIControl { ...@@ -334,22 +334,22 @@ open class SwitchControl: UIControl {
- Parameter on: A bool of whether the switch should be in the on state or not. - Parameter on: A bool of whether the switch should be in the on state or not.
- Parameter animated: A Boolean indicating to set the animation or not. - Parameter animated: A Boolean indicating to set the animation or not.
*/ */
public func setOn(on: Bool, animated: Bool, completion: ((SwitchControl) -> Void)? = nil) { public func setOn(on: Bool, animated: Bool, completion: ((Switch) -> Void)? = nil) {
setSwitchControlState(state: on ? .on : .off, animated: animated, completion: completion) setSwitchState(state: on ? .on : .off, animated: animated, completion: completion)
} }
/** /**
Set the switchState property with an option to animate. Set the switchState property with an option to animate.
- Parameter state: The SwitchControlState to set. - Parameter state: The SwitchState to set.
- Parameter animated: A Boolean indicating to set the animation or not. - Parameter animated: A Boolean indicating to set the animation or not.
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
public func setSwitchControlState(state: SwitchControlState, animated: Bool = true, completion: ((SwitchControl) -> Void)? = nil) { public func setSwitchState(state: SwitchState, animated: Bool = true, completion: ((Switch) -> Void)? = nil) {
if isEnabled && internalSwitchControlState != state { if isEnabled && internalSwitchState != state {
internalSwitchControlState = state internalSwitchState = state
if animated { if animated {
animateToState(state: state) { [weak self] _ in animateToState(state: state) { [weak self] _ in
if let s: SwitchControl = self { if let s: Switch = self {
s.sendActions(for: .valueChanged) s.sendActions(for: .valueChanged)
completion?(s) completion?(s)
s.delegate?.switchStateChanged(control: s) s.delegate?.switchStateChanged(control: s)
...@@ -374,7 +374,7 @@ open class SwitchControl: UIControl { ...@@ -374,7 +374,7 @@ open class SwitchControl: UIControl {
internal func handleTouchUpOutsideOrCanceled(sender: FabButton, event: UIEvent) { internal func handleTouchUpOutsideOrCanceled(sender: FabButton, event: UIEvent) {
if let v: UITouch = event.touches(for: sender)?.first { if let v: UITouch = event.touches(for: sender)?.first {
let q: CGFloat = sender.x + v.location(in: sender).x - v.previousLocation(in: sender).x let q: CGFloat = sender.x + v.location(in: sender).x - v.previousLocation(in: sender).x
setSwitchControlState(state: q > (width - button.width) / 2 ? .on : .off, animated: true) setSwitchState(state: q > (width - button.width) / 2 ? .on : .off, animated: true)
} }
} }
...@@ -401,7 +401,7 @@ open class SwitchControl: UIControl { ...@@ -401,7 +401,7 @@ open class SwitchControl: UIControl {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if true == trackLayer.frame.contains(layer.convert(touches.first!.location(in: self), from: layer)) { if true == trackLayer.frame.contains(layer.convert(touches.first!.location(in: self), from: layer)) {
setOn(on: .on != internalSwitchControlState, animated: true) setOn(on: .on != internalSwitchState, animated: true)
} }
} }
...@@ -423,35 +423,35 @@ open class SwitchControl: UIControl { ...@@ -423,35 +423,35 @@ open class SwitchControl: UIControl {
/** /**
Prepares the switchState property. This is used mainly to allow Prepares the switchState property. This is used mainly to allow
init to set the state value and have an effect. init to set the state value and have an effect.
- Parameter state: The SwitchControlState to set. - Parameter state: The SwitchState to set.
*/ */
private func prepareSwitchControlState(state: SwitchControlState) { private func prepareSwitchState(state: SwitchState) {
setSwitchControlState(state: state, animated: false) setSwitchState(state: state, animated: false)
} }
/** /**
Prepares the switchStyle property. This is used mainly to allow Prepares the switchStyle property. This is used mainly to allow
init to set the state value and have an effect. init to set the state value and have an effect.
- Parameter style: The SwitchControlStyle to set. - Parameter style: The SwitchStyle to set.
*/ */
private func prepareSwitchControlStyle(style: SwitchControlStyle) { private func prepareSwitchStyle(style: SwitchStyle) {
switchStyle = style switchStyle = style
} }
/** /**
Prepares the switchSize property. This is used mainly to allow Prepares the switchSize property. This is used mainly to allow
init to set the size value and have an effect. init to set the size value and have an effect.
- Parameter size: The SwitchControlSize to set. - Parameter size: The SwitchSize to set.
*/ */
private func prepareSwitchControlSize(size: SwitchControlSize) { private func prepareSwitchSize(size: SwitchSize) {
switchSize = size switchSize = size
} }
/** /**
Updates the style based on the state. Updates the style based on the state.
- Parameter state: The SwitchControlState to set the style to. - Parameter state: The SwitchState to set the style to.
*/ */
private func styleForState(state: SwitchControlState) { private func styleForState(state: SwitchState) {
if isEnabled { if isEnabled {
updateColorForState(state: state) updateColorForState(state: state)
} else { } else {
...@@ -461,9 +461,9 @@ open class SwitchControl: UIControl { ...@@ -461,9 +461,9 @@ open class SwitchControl: UIControl {
/** /**
Updates the coloring for the enabled state. Updates the coloring for the enabled state.
- Parameter state: SwitchControlState. - Parameter state: SwitchState.
*/ */
private func updateColorForState(state: SwitchControlState) { private func updateColorForState(state: SwitchState) {
if .on == state { if .on == state {
button.backgroundColor = buttonOnColor button.backgroundColor = buttonOnColor
trackLayer.backgroundColor = trackOnColor.cgColor trackLayer.backgroundColor = trackOnColor.cgColor
...@@ -475,9 +475,9 @@ open class SwitchControl: UIControl { ...@@ -475,9 +475,9 @@ open class SwitchControl: UIControl {
/** /**
Updates the coloring for the disabled state. Updates the coloring for the disabled state.
- Parameter state: SwitchControlState. - Parameter state: SwitchState.
*/ */
private func updateColorForDisabledState(state: SwitchControlState) { private func updateColorForDisabledState(state: SwitchState) {
if .on == state { if .on == state {
button.backgroundColor = buttonOnDisabledColor button.backgroundColor = buttonOnDisabledColor
trackLayer.backgroundColor = trackOnDisabledColor.cgColor trackLayer.backgroundColor = trackOnDisabledColor.cgColor
...@@ -488,7 +488,7 @@ open class SwitchControl: UIControl { ...@@ -488,7 +488,7 @@ open class SwitchControl: UIControl {
} }
/// Laout the button and track views. /// Laout the button and track views.
private func layoutSwitchControl() { private func layoutSwitch() {
var w: CGFloat = 0 var w: CGFloat = 0
switch switchSize { switch switchSize {
case .small: case .small:
...@@ -508,34 +508,34 @@ open class SwitchControl: UIControl { ...@@ -508,34 +508,34 @@ open class SwitchControl: UIControl {
onPosition = width - px - buttonDiameter onPosition = width - px - buttonDiameter
offPosition = px offPosition = px
if .on == internalSwitchControlState { if .on == internalSwitchState {
button.x = onPosition button.x = onPosition
} }
} }
/** /**
Set the switchState property with an animate. Set the switchState property with an animate.
- Parameter state: The SwitchControlState to set. - Parameter state: The SwitchState to set.
- Parameter completion: An Optional completion block. - Parameter completion: An Optional completion block.
*/ */
private func animateToState(state: SwitchControlState, completion: ((SwitchControl) -> Void)? = nil) { private func animateToState(state: SwitchState, completion: ((Switch) -> Void)? = nil) {
isUserInteractionEnabled = false isUserInteractionEnabled = false
UIView.animate(withDuration: 0.15, UIView.animate(withDuration: 0.15,
delay: 0.05, delay: 0.05,
options: [.curveEaseIn, .curveEaseOut], options: [.curveEaseIn, .curveEaseOut],
animations: { [weak self] in animations: { [weak self] in
if let s: SwitchControl = self { if let s: Switch = self {
s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset
s.styleForState(state: state) s.styleForState(state: state)
} }
}) { [weak self] _ in }) { [weak self] _ in
UIView.animate(withDuration: 0.15, UIView.animate(withDuration: 0.15,
animations: { [weak self] in animations: { [weak self] in
if let s: SwitchControl = self { if let s: Switch = self {
s.button.x = .on == state ? s.onPosition : s.offPosition s.button.x = .on == state ? s.onPosition : s.offPosition
} }
}) { [weak self] _ in }) { [weak self] _ in
if let s: SwitchControl = self { if let s: Switch = self {
s.isUserInteractionEnabled = true s.isUserInteractionEnabled = true
completion?(s) completion?(s)
} }
......
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