Commit ca919cfe by Daniel Dahan

development: updated MenuView and Menu to now be one object

parent 159165b9
......@@ -71,8 +71,7 @@
96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78A1CB40DC500C806FE /* MaterialTextLayer.swift */; };
96BCB7D41CB40DC500C806FE /* TransitionAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78B1CB40DC500C806FE /* TransitionAnimation.swift */; };
96BCB7D51CB40DC500C806FE /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78C1CB40DC500C806FE /* View.swift */; };
96BCB7D61CB40DC500C806FE /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78D1CB40DC500C806FE /* Menu.swift */; };
96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* MenuView.swift */; };
96BCB7D71CB40DC500C806FE /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* Menu.swift */; };
96BCB7D81CB40DC500C806FE /* MenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78F1CB40DC500C806FE /* MenuController.swift */; };
96BCB7D91CB40DC500C806FE /* NavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7901CB40DC500C806FE /* NavigationBar.swift */; };
96BCB7DA1CB40DC500C806FE /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7911CB40DC500C806FE /* NavigationController.swift */; };
......@@ -140,8 +139,7 @@
96BCB83B1CB4115200C806FE /* NavigationDrawerController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7971CB40DC500C806FE /* NavigationDrawerController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83C1CB4115200C806FE /* BarView.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7981CB40DC500C806FE /* BarView.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83D1CB4115200C806FE /* RootController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7991CB40DC500C806FE /* RootController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83E1CB4115200C806FE /* Menu.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78D1CB40DC500C806FE /* Menu.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83F1CB4115200C806FE /* MenuView.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* MenuView.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83F1CB4115200C806FE /* Menu.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* Menu.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8401CB4115200C806FE /* MenuController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78F1CB40DC500C806FE /* MenuController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8411CB4115200C806FE /* TabBar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB79A1CB40DC500C806FE /* TabBar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8421CB4115200C806FE /* Toolbar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB79F1CB40DC500C806FE /* Toolbar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -266,8 +264,7 @@
96BCB78A1CB40DC500C806FE /* MaterialTextLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialTextLayer.swift; sourceTree = "<group>"; };
96BCB78B1CB40DC500C806FE /* TransitionAnimation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransitionAnimation.swift; sourceTree = "<group>"; };
96BCB78C1CB40DC500C806FE /* View.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = View.swift; sourceTree = "<group>"; };
96BCB78D1CB40DC500C806FE /* Menu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = "<group>"; };
96BCB78E1CB40DC500C806FE /* MenuView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = MenuView.swift; sourceTree = "<group>"; tabWidth = 4; };
96BCB78E1CB40DC500C806FE /* Menu.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = "<group>"; tabWidth = 4; };
96BCB78F1CB40DC500C806FE /* MenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuController.swift; sourceTree = "<group>"; };
96BCB7901CB40DC500C806FE /* NavigationBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationBar.swift; sourceTree = "<group>"; };
96BCB7911CB40DC500C806FE /* NavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationController.swift; sourceTree = "<group>"; };
......@@ -436,8 +433,7 @@
963FBF021D6696D0008F8512 /* Menu */ = {
isa = PBXGroup;
children = (
96BCB78D1CB40DC500C806FE /* Menu.swift */,
96BCB78E1CB40DC500C806FE /* MenuView.swift */,
96BCB78E1CB40DC500C806FE /* Menu.swift */,
96BCB78F1CB40DC500C806FE /* MenuController.swift */,
);
name = Menu;
......@@ -818,8 +814,7 @@
96BCB83B1CB4115200C806FE /* NavigationDrawerController.swift in Headers */,
96BCB83C1CB4115200C806FE /* BarView.swift in Headers */,
96BCB83D1CB4115200C806FE /* RootController.swift in Headers */,
96BCB83E1CB4115200C806FE /* Menu.swift in Headers */,
96BCB83F1CB4115200C806FE /* MenuView.swift in Headers */,
96BCB83F1CB4115200C806FE /* Menu.swift in Headers */,
96BCB8401CB4115200C806FE /* MenuController.swift in Headers */,
96BCB8411CB4115200C806FE /* TabBar.swift in Headers */,
96BCB8421CB4115200C806FE /* Toolbar.swift in Headers */,
......@@ -1051,7 +1046,6 @@
96BCB7DF1CB40DC500C806FE /* SearchBarController.swift in Sources */,
96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */,
96BCB7C11CB40DC500C806FE /* Depth.swift in Sources */,
96BCB7D61CB40DC500C806FE /* Menu.swift in Sources */,
96BCB7E61CB40DC500C806FE /* TextStorage.swift in Sources */,
96BCB7A91CB40DC500C806FE /* FlatButton.swift in Sources */,
96BCB7CA1CB40DC500C806FE /* Layout.swift in Sources */,
......@@ -1101,7 +1095,7 @@
96BCB7E21CB40DC500C806FE /* RootController.swift in Sources */,
96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */,
96BCB7DD1CB40DC500C806FE /* RobotoFont.swift in Sources */,
96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */,
96BCB7D71CB40DC500C806FE /* Menu.swift in Sources */,
966ECF2A1CF4C20100BB0BDF /* CollectionReusableView.swift in Sources */,
96BCB7E51CB40DC500C806FE /* TextField.swift in Sources */,
96BCB7D21CB40DC500C806FE /* TableViewCell.swift in Sources */,
......
......@@ -38,508 +38,544 @@ public enum MenuDirection: Int {
case right
}
public class Menu {
/// A Boolean that indicates if the menu is open or not.
public private(set) var isOpened = false
/// The rectangular bounds that the menu animates.
public var origin: CGPoint {
didSet {
reload()
}
}
@objc(MenuDelegate)
public protocol MenuDelegate {
/// Gets called when the user taps outside menu buttons.
@objc
optional func menu(menu: Menu, tappedAt point: CGPoint)
}
/// A preset wrapper around interimSpace.
public var interimSpacePreset = InterimSpacePreset.none {
didSet {
open class Menu: View {
/// A Boolean that indicates if the menu is open or not.
open private(set) var isOpened = false
/// A preset wrapper around interimSpace.
open var interimSpacePreset = InterimSpacePreset.none {
didSet {
interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset)
}
}
/// The space between views.
public var interimSpace: InterimSpace {
didSet {
reload()
}
}
/// Enables the animations for the Menu.
public var isEnabled = true
/// The direction in which the animation opens the menu.
public var direction: MenuDirection = .up {
didSet {
reload()
}
}
/// An Array of UIViews.
public var views: [UIView]? {
didSet {
reload()
}
}
/// Size of views, not including the first view.
public var itemSize: CGSize = CGSize(width: 48, height: 48)
/// An Optional base view size.
public var baseSize: CGSize?
/**
Initializer.
- Parameter origin: The origin position of the Menu.
- Parameter interimSpace: The interimSpace size between views.
*/
public init(origin: CGPoint, interimSpace: InterimSpace = 16) {
self.origin = origin
self.interimSpace = interimSpace
}
/// Convenience initializer.
public convenience init() {
self.init(origin: .zero)
}
/// Reload the view layout.
public func reload() {
isOpened = false
layoutButtons()
}
/**
Open the Menu component with animation options.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func open(duration: TimeInterval = 0.15, delay: TimeInterval = 0, usingSpringWithDamping: CGFloat = 0.5, initialSpringVelocity: CGFloat = 0, options: UIViewAnimationOptions = [], animations: ((UIView) -> Void)? = nil, completion: ((UIView) -> Void)? = nil) {
if isEnabled {
disable()
switch direction {
case .up:
openUpAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .down:
openDownAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .left:
openLeftAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .right:
openRightAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
}
}
}
/**
Close the Menu component with animation options.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func close(duration: TimeInterval = 0.15, delay: TimeInterval = 0, usingSpringWithDamping: CGFloat = 0.5, initialSpringVelocity: CGFloat = 0, options: UIViewAnimationOptions = [], animations: ((UIView) -> Void)? = nil, completion: ((UIView) -> Void)? = nil) {
if isEnabled {
disable()
switch direction {
case .up:
closeUpAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .down:
closeDownAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .left:
closeLeftAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .right:
closeRightAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
}
}
}
/**
Open the Menu component with animation options in the Up direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openUpAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
var base: UIView?
for i in 1..<v.count {
if nil == base {
base = v[0]
}
let view: UIView = v[i]
view.isHidden = false
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 1
view.y = base!.y - CGFloat(i) * s.itemSize.height - CGFloat(i) * s.interimSpace
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
s.enable(view: view)
if view == v.last {
s.isOpened = true
}
completion?(view)
}
}
}
}
}
/**
Close the Menu component with animation options in the Up direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func closeUpAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
for i in 1..<v.count {
let view: UIView = v[i]
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 0
view.y = s.origin.y
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
view.isHidden = true
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
}
}
}
}
}
/**
Open the Menu component with animation options in the Down direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openDownAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
var base: UIView?
for i in 1..<v.count {
if nil == base {
base = v[0]
}
let view: UIView = v[i]
view.isHidden = false
let h: CGFloat = nil == baseSize ? itemSize.height : baseSize!.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 1
view.y = base!.y + h + CGFloat(i - 1) * s.itemSize.height + CGFloat(i) * s.interimSpace
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
s.enable(view: view)
if view == v.last {
s.isOpened = true
}
completion?(view)
}
}
}
}
}
/**
Close the Menu component with animation options in the Down direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func closeDownAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
for i in 1..<v.count {
let view: UIView = v[i]
let h: CGFloat = nil == baseSize ? itemSize.height : baseSize!.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 0
view.y = s.origin.y + h
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
view.isHidden = true
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
}
}
}
}
}
/**
Open the Menu component with animation options in the Left direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openLeftAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
var base: UIView?
for i in 1..<v.count {
if nil == base {
base = v[0]
}
let view: UIView = v[i]
view.isHidden = false
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 1
view.x = base!.x - CGFloat(i) * s.itemSize.width - CGFloat(i) * s.interimSpace
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
s.enable(view: view)
if view == v.last {
s.isOpened = true
}
completion?(view)
}
}
}
}
}
/**
Close the Menu component with animation options in the Left direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func closeLeftAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
for i in 1..<v.count {
let view: UIView = v[i]
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 0
view.x = s.origin.x
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
view.isHidden = true
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
}
}
}
}
}
/**
Open the Menu component with animation options in the Right direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openRightAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
var base: UIView?
for i in 1..<v.count {
if nil == base {
base = v[0]
}
let view: UIView = v[i]
view.isHidden = false
let h: CGFloat = nil == baseSize ? itemSize.height : baseSize!.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 1
view.x = base!.x + h + CGFloat(i - 1) * s.itemSize.width + CGFloat(i) * s.interimSpace
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
s.enable(view: view)
if view == v.last {
s.isOpened = true
}
completion?(view)
}
}
}
}
}
/**
Close the Menu component with animation options in the Right direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
public func closeRightAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views {
for i in 1..<v.count {
let view: UIView = v[i]
let w: CGFloat = nil == baseSize ? itemSize.width : baseSize!.width
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
if let s: Menu = self {
view.alpha = 0
view.x = s.origin.x + w
animations?(view)
}
}) { [weak self] _ in
if let s: Menu = self {
view.isHidden = true
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
}
}
}
}
}
/// Layout the views.
private func layoutButtons() {
if let v = views {
let size: CGSize = nil == baseSize ? itemSize : baseSize!
for i in 0..<v.count {
let view: UIView = v[i]
if 0 == i {
view.frame.size = size
view.frame.origin = origin
view.layer.zPosition = 10000
} else {
view.alpha = 0
view.isHidden = true
view.frame.size = itemSize
view.x = origin.x + (size.width - itemSize.width) / 2
view.y = origin.y + (size.height - itemSize.height) / 2
view.layer.zPosition = CGFloat(10000 - v.count - i)
}
}
}
}
/// Disable the Menu if views exist.
private func disable() {
if let v = views {
if 0 < v.count {
isEnabled = false
}
}
}
/**
Enable the Menu if the last view is equal to the passed in view.
- Parameter view: UIView that is passed in to compare.
*/
private func enable(view: UIView) {
if let v = views {
if view == v.last {
isEnabled = true
}
}
}
}
}
/// The space between views.
open var interimSpace: InterimSpace = 0 {
didSet {
reload()
}
}
/// Enables the animations for the Menu.
open private(set) var isEnabled = true
/// The direction in which the animation opens the menu.
open var direction = MenuDirection.up {
didSet {
reload()
}
}
/// An Array of UIViews.
open var views = [UIView]() {
didSet {
reload()
}
}
/// An Optional base view size.
open var baseSize = CGSize(width: 48, height: 48)
/// Size of views, not including the first view.
open var itemSize = CGSize(width: 48, height: 48)
/**
Initializer.
- Parameter coder aDecoder: An NSCoder.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/**
Initializer.
- Parameter interimSpace: The interimSpace size between views.
*/
public convenience init(interimSpace: InterimSpace = 16) {
self.init()
self.interimSpace = interimSpace
}
/// Reload the view layout.
open func reload() {
isOpened = false
layoutButtons()
}
/**
Open the Menu component with animation options.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
open func open(duration: TimeInterval = 0.15, delay: TimeInterval = 0, usingSpringWithDamping: CGFloat = 0.5, initialSpringVelocity: CGFloat = 0, options: UIViewAnimationOptions = [], animations: ((UIView) -> Void)? = nil, completion: ((UIView) -> Void)? = nil) {
if isEnabled {
disable()
switch direction {
case .up:
openUpAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .down:
openDownAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .left:
openLeftAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .right:
openRightAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
}
}
}
/**
Close the Menu component with animation options.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
open func close(duration: TimeInterval = 0.15, delay: TimeInterval = 0, usingSpringWithDamping: CGFloat = 0.5, initialSpringVelocity: CGFloat = 0, options: UIViewAnimationOptions = [], animations: ((UIView) -> Void)? = nil, completion: ((UIView) -> Void)? = nil) {
if isEnabled {
disable()
switch direction {
case .up:
closeUpAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .down:
closeDownAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .left:
closeLeftAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
case .right:
closeRightAnimation(duration: duration, delay: delay, usingSpringWithDamping: usingSpringWithDamping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: completion)
}
}
}
/**
Open the Menu component with animation options in the Up direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openUpAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
guard let base = views.first else {
return
}
for i in 1..<views.count {
let v = views[i]
v.isHidden = false
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self, base = base] in
guard let s = self else {
return
}
v.alpha = 1
v.y = base.y - CGFloat(i) * s.itemSize.height - CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
}
completion?(v)
}
}
}
/**
Close the Menu component with animation options in the Up direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func closeUpAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
for i in 1..<views.count {
let v = views[i]
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
guard let s = self else {
return
}
v.alpha = 0
v.y = s.y
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
}
completion?(v)
}
}
}
/**
Open the Menu component with animation options in the Down direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openDownAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
guard let base = views.first else {
return
}
for i in 1..<views.count {
let v = views[i]
v.isHidden = false
let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self, base = base] in
guard let s = self else {
return
}
v.alpha = 1
v.y = base.y + h + CGFloat(i - 1) * s.itemSize.height + CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
}
completion?(v)
}
}
}
/**
Close the Menu component with animation options in the Down direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func closeDownAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
for i in 1..<views.count {
let v = views[i]
let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
guard let s = self else {
return
}
v.alpha = 0
v.y = s.y + h
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
}
completion?(v)
}
}
}
/**
Open the Menu component with animation options in the Left direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openLeftAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
guard let base = views.first else {
return
}
for i in 1..<views.count {
let v = views[i]
v.isHidden = false
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self, base = base] in
guard let s = self else {
return
}
v.alpha = 1
v.x = base.x - CGFloat(i) * s.itemSize.width - CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
}
completion?(v)
}
}
}
/**
Close the Menu component with animation options in the Left direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func closeLeftAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
for i in 1..<views.count {
let v = views[i]
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
guard let s = self else {
return
}
v.alpha = 0
v.x = s.x
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
}
completion?(v)
}
}
}
/**
Open the Menu component with animation options in the Right direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func openRightAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
guard let base = views.first else {
return
}
for i in 1..<views.count {
let v = views[i]
v.isHidden = false
let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self, base = base] in
guard let s = self else {
return
}
v.alpha = 1
v.x = base.x + h + CGFloat(i - 1) * s.itemSize.width + CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
}
completion?(v)
}
}
}
/**
Close the Menu component with animation options in the Right direction.
- Parameter duration: The time for each view's animation.
- Parameter delay: A delay time for each view's animation.
- Parameter usingSpringWithDamping: A damping ratio for the animation.
- Parameter initialSpringVelocity: The initial velocity for the animation.
- Parameter options: Options to pass to the animation.
- Parameter animations: An animation block to execute on each view's animation.
- Parameter completion: A completion block to execute on each view's animation.
*/
private func closeRightAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
for i in 1..<views.count {
let v = views[i]
let w = baseSize.width
UIView.animate(withDuration: Double(i) * duration,
delay: delay,
usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity,
options: options,
animations: { [weak self] in
guard let s = self else {
return
}
v.alpha = 0
v.x = s.x + w
animations?(v)
}) { [weak self] _ in
guard let s = self else {
return
}
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
}
completion?(v)
}
}
}
/// Layout the views.
private func layoutButtons() {
for i in 0..<views.count {
let v = views[i]
if 0 == i {
v.frame.size = baseSize
v.layer.zPosition = 10000
} else {
v.alpha = 0
v.isHidden = true
v.frame.size = itemSize
v.x = x + (baseSize.width - itemSize.width) / 2
v.y = y + (baseSize.height - itemSize.height) / 2
v.layer.zPosition = CGFloat(10000 - views.count - i)
}
}
}
/// Disable the Menu if views exist.
private func disable() {
guard 0 < views.count else {
return
}
isEnabled = false
}
/**
Enable the Menu if the last view is equal to the passed in view.
- Parameter view: UIView that is passed in to compare.
*/
private func enable(view: UIView) {
guard view == views.last else {
return
}
isEnabled = true
}
}
......@@ -49,63 +49,77 @@ extension UIViewController {
}
@IBDesignable
public class MenuController: RootController {
open class MenuController: RootController {
/// Reference to the MenuView.
public private(set) lazy var menuView: MenuView = MenuView()
open internal(set) lazy var menu: Menu = Menu()
/**
Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been opened.
*/
public func openMenu(completion: (() -> Void)? = nil) {
Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been opened.
*/
open func open(completion: (() -> Void)? = nil) {
if true == isUserInteractionEnabled {
isUserInteractionEnabled = false
rootViewController.view.alpha = 0.5
menuView.open(completion: completion)
menu.open { [weak self] (view) in
guard let s = self else {
return
}
if view == s.menu.views.last {
completion?()
}
}
}
}
/**
Closes the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been closed.
*/
public func closeMenu(completion: (() -> Void)? = nil) {
/**
Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been closed.
*/
open func close(completion: (() -> Void)? = nil) {
if false == isUserInteractionEnabled {
rootViewController.view.alpha = 1
menuView.close(completion: { [weak self] in
self?.isUserInteractionEnabled = true
completion?()
})
menu.open { [weak self] (view) in
guard let s = self else {
return
}
if view == s.menu.views.last {
s.isUserInteractionEnabled = true
completion?()
}
}
}
}
/**
To execute in the order of the layout chain, override this
method. LayoutSubviews should be called immediately, unless you
have a certain need.
*/
public override func layoutSubviews() {
To execute in the order of the layout chain, override this
method. LayoutSubviews should be called immediately, unless you
have a certain need.
*/
open override func layoutSubviews() {
super.layoutSubviews()
rootViewController.view.frame = view.bounds
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public override func prepareView() {
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
open override func prepareView() {
super.prepareView()
prepareMenuView()
}
/// Prepares the MenuView.
private func prepareMenuView() {
menuView.zPosition = 1000
view.addSubview(menuView)
menu.zPosition = 1000
view.addSubview(menu)
}
}
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
@objc(MenuViewDelegate)
public protocol MenuViewDelegate {
/// Gets called when the user taps outside menu buttons.
@objc
optional func menuViewDidTapOutside(menuView: MenuView)
}
open class MenuView : PulseView {
/// References the Menu instance.
open internal(set) lazy var menu: Menu = Menu()
/// A delegation reference.
open weak var delegate: MenuViewDelegate?
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
/**
Since the subviews will be outside the bounds of this view,
we need to look at the subviews to see if we have a hit.
*/
guard !isHidden else {
return nil
}
for v in subviews {
let p = v.convert(point, from: self)
if v.bounds.contains(p) {
return v.hitTest(p, with: event)
}
}
if menu.isOpened {
delegate?.menuViewDidTapOutside?(menuView: self)
}
return super.hitTest(point, with: event)
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
open override func prepareView() {
super.prepareView()
pulseAnimation = .none
clipsToBounds = false
backgroundColor = nil
}
/**
Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been opened.
*/
open func open(completion: (() -> Void)? = nil) {
if true == menu.views?.first?.isUserInteractionEnabled {
menu.views?.first?.isUserInteractionEnabled = false
menu.open { [weak self] (v: UIView) in
if self?.menu.views?.last == v {
self?.menu.views?.first?.isUserInteractionEnabled = true
completion?()
}
}
}
}
/**
Closes the menu with a callback.
- Parameter completion: An Optional callback that is executed when
all menu items have been closed.
*/
open func close(completion: (() -> Void)? = nil) {
if true == menu.views?.first?.isUserInteractionEnabled {
menu.views?.first?.isUserInteractionEnabled = false
menu.close { [weak self] (v: UIView) in
if self?.menu.views?.last == v {
self?.menu.views?.first?.isUserInteractionEnabled = true
completion?()
}
}
}
}
}
......@@ -30,7 +30,7 @@
import UIKit
open class PhotoLibraryController: UIViewController, PhotoLibraryDelegate {
open class PhotoLibraryController: UIViewController {
/// A reference to a PhotoLibrary.
public private(set) lazy var photoLibrary: PhotoLibrary = PhotoLibrary()
......@@ -58,3 +58,5 @@ open class PhotoLibraryController: UIViewController, PhotoLibraryDelegate {
photoLibrary.delegate = self
}
}
extension PhotoLibraryController: PhotoLibraryDelegate {}
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