Commit ca919cfe by Daniel Dahan

development: updated MenuView and Menu to now be one object

parent 159165b9
...@@ -71,8 +71,7 @@ ...@@ -71,8 +71,7 @@
96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78A1CB40DC500C806FE /* MaterialTextLayer.swift */; }; 96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78A1CB40DC500C806FE /* MaterialTextLayer.swift */; };
96BCB7D41CB40DC500C806FE /* TransitionAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78B1CB40DC500C806FE /* TransitionAnimation.swift */; }; 96BCB7D41CB40DC500C806FE /* TransitionAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78B1CB40DC500C806FE /* TransitionAnimation.swift */; };
96BCB7D51CB40DC500C806FE /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78C1CB40DC500C806FE /* View.swift */; }; 96BCB7D51CB40DC500C806FE /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78C1CB40DC500C806FE /* View.swift */; };
96BCB7D61CB40DC500C806FE /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78D1CB40DC500C806FE /* Menu.swift */; }; 96BCB7D71CB40DC500C806FE /* Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* Menu.swift */; };
96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* MenuView.swift */; };
96BCB7D81CB40DC500C806FE /* MenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78F1CB40DC500C806FE /* MenuController.swift */; }; 96BCB7D81CB40DC500C806FE /* MenuController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB78F1CB40DC500C806FE /* MenuController.swift */; };
96BCB7D91CB40DC500C806FE /* NavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7901CB40DC500C806FE /* NavigationBar.swift */; }; 96BCB7D91CB40DC500C806FE /* NavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7901CB40DC500C806FE /* NavigationBar.swift */; };
96BCB7DA1CB40DC500C806FE /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7911CB40DC500C806FE /* NavigationController.swift */; }; 96BCB7DA1CB40DC500C806FE /* NavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7911CB40DC500C806FE /* NavigationController.swift */; };
...@@ -140,8 +139,7 @@ ...@@ -140,8 +139,7 @@
96BCB83B1CB4115200C806FE /* NavigationDrawerController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7971CB40DC500C806FE /* NavigationDrawerController.swift */; settings = {ATTRIBUTES = (Public, ); }; }; 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, ); }; }; 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, ); }; }; 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 /* Menu.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* Menu.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83F1CB4115200C806FE /* MenuView.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78E1CB40DC500C806FE /* MenuView.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8401CB4115200C806FE /* MenuController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78F1CB40DC500C806FE /* MenuController.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, ); }; }; 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, ); }; }; 96BCB8421CB4115200C806FE /* Toolbar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB79F1CB40DC500C806FE /* Toolbar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
...@@ -266,8 +264,7 @@ ...@@ -266,8 +264,7 @@
96BCB78A1CB40DC500C806FE /* MaterialTextLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialTextLayer.swift; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 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 /* Menu.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = Menu.swift; sourceTree = "<group>"; tabWidth = 4; };
96BCB78E1CB40DC500C806FE /* MenuView.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = MenuView.swift; sourceTree = "<group>"; tabWidth = 4; };
96BCB78F1CB40DC500C806FE /* MenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuController.swift; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 96BCB7911CB40DC500C806FE /* NavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationController.swift; sourceTree = "<group>"; };
...@@ -436,8 +433,7 @@ ...@@ -436,8 +433,7 @@
963FBF021D6696D0008F8512 /* Menu */ = { 963FBF021D6696D0008F8512 /* Menu */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
96BCB78D1CB40DC500C806FE /* Menu.swift */, 96BCB78E1CB40DC500C806FE /* Menu.swift */,
96BCB78E1CB40DC500C806FE /* MenuView.swift */,
96BCB78F1CB40DC500C806FE /* MenuController.swift */, 96BCB78F1CB40DC500C806FE /* MenuController.swift */,
); );
name = Menu; name = Menu;
...@@ -818,8 +814,7 @@ ...@@ -818,8 +814,7 @@
96BCB83B1CB4115200C806FE /* NavigationDrawerController.swift in Headers */, 96BCB83B1CB4115200C806FE /* NavigationDrawerController.swift in Headers */,
96BCB83C1CB4115200C806FE /* BarView.swift in Headers */, 96BCB83C1CB4115200C806FE /* BarView.swift in Headers */,
96BCB83D1CB4115200C806FE /* RootController.swift in Headers */, 96BCB83D1CB4115200C806FE /* RootController.swift in Headers */,
96BCB83E1CB4115200C806FE /* Menu.swift in Headers */, 96BCB83F1CB4115200C806FE /* Menu.swift in Headers */,
96BCB83F1CB4115200C806FE /* MenuView.swift in Headers */,
96BCB8401CB4115200C806FE /* MenuController.swift in Headers */, 96BCB8401CB4115200C806FE /* MenuController.swift in Headers */,
96BCB8411CB4115200C806FE /* TabBar.swift in Headers */, 96BCB8411CB4115200C806FE /* TabBar.swift in Headers */,
96BCB8421CB4115200C806FE /* Toolbar.swift in Headers */, 96BCB8421CB4115200C806FE /* Toolbar.swift in Headers */,
...@@ -1051,7 +1046,6 @@ ...@@ -1051,7 +1046,6 @@
96BCB7DF1CB40DC500C806FE /* SearchBarController.swift in Sources */, 96BCB7DF1CB40DC500C806FE /* SearchBarController.swift in Sources */,
96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */, 96BCB7D31CB40DC500C806FE /* MaterialTextLayer.swift in Sources */,
96BCB7C11CB40DC500C806FE /* Depth.swift in Sources */, 96BCB7C11CB40DC500C806FE /* Depth.swift in Sources */,
96BCB7D61CB40DC500C806FE /* Menu.swift in Sources */,
96BCB7E61CB40DC500C806FE /* TextStorage.swift in Sources */, 96BCB7E61CB40DC500C806FE /* TextStorage.swift in Sources */,
96BCB7A91CB40DC500C806FE /* FlatButton.swift in Sources */, 96BCB7A91CB40DC500C806FE /* FlatButton.swift in Sources */,
96BCB7CA1CB40DC500C806FE /* Layout.swift in Sources */, 96BCB7CA1CB40DC500C806FE /* Layout.swift in Sources */,
...@@ -1101,7 +1095,7 @@ ...@@ -1101,7 +1095,7 @@
96BCB7E21CB40DC500C806FE /* RootController.swift in Sources */, 96BCB7E21CB40DC500C806FE /* RootController.swift in Sources */,
96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */, 96BCB7DC1CB40DC500C806FE /* RaisedButton.swift in Sources */,
96BCB7DD1CB40DC500C806FE /* RobotoFont.swift in Sources */, 96BCB7DD1CB40DC500C806FE /* RobotoFont.swift in Sources */,
96BCB7D71CB40DC500C806FE /* MenuView.swift in Sources */, 96BCB7D71CB40DC500C806FE /* Menu.swift in Sources */,
966ECF2A1CF4C20100BB0BDF /* CollectionReusableView.swift in Sources */, 966ECF2A1CF4C20100BB0BDF /* CollectionReusableView.swift in Sources */,
96BCB7E51CB40DC500C806FE /* TextField.swift in Sources */, 96BCB7E51CB40DC500C806FE /* TextField.swift in Sources */,
96BCB7D21CB40DC500C806FE /* TableViewCell.swift in Sources */, 96BCB7D21CB40DC500C806FE /* TableViewCell.swift in Sources */,
......
...@@ -38,71 +38,73 @@ public enum MenuDirection: Int { ...@@ -38,71 +38,73 @@ public enum MenuDirection: Int {
case right case right
} }
public class Menu { @objc(MenuDelegate)
/// A Boolean that indicates if the menu is open or not. public protocol MenuDelegate {
public private(set) var isOpened = false /// Gets called when the user taps outside menu buttons.
@objc
optional func menu(menu: Menu, tappedAt point: CGPoint)
}
/// The rectangular bounds that the menu animates. open class Menu: View {
public var origin: CGPoint { /// A Boolean that indicates if the menu is open or not.
didSet { open private(set) var isOpened = false
reload()
}
}
/// A preset wrapper around interimSpace. /// A preset wrapper around interimSpace.
public var interimSpacePreset = InterimSpacePreset.none { open var interimSpacePreset = InterimSpacePreset.none {
didSet { didSet {
interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset) interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset)
} }
} }
/// The space between views. /// The space between views.
public var interimSpace: InterimSpace { open var interimSpace: InterimSpace = 0 {
didSet { didSet {
reload() reload()
} }
} }
/// Enables the animations for the Menu. /// Enables the animations for the Menu.
public var isEnabled = true open private(set) var isEnabled = true
/// The direction in which the animation opens the menu. /// The direction in which the animation opens the menu.
public var direction: MenuDirection = .up { open var direction = MenuDirection.up {
didSet { didSet {
reload() reload()
} }
} }
/// An Array of UIViews. /// An Array of UIViews.
public var views: [UIView]? { open var views = [UIView]() {
didSet { didSet {
reload() reload()
} }
} }
/// An Optional base view size.
open var baseSize = CGSize(width: 48, height: 48)
/// Size of views, not including the first view. /// Size of views, not including the first view.
public var itemSize: CGSize = CGSize(width: 48, height: 48) open var itemSize = CGSize(width: 48, height: 48)
/// An Optional base view size. /**
public var baseSize: CGSize? Initializer.
- Parameter coder aDecoder: An NSCoder.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/** /**
Initializer. Initializer.
- Parameter origin: The origin position of the Menu.
- Parameter interimSpace: The interimSpace size between views. - Parameter interimSpace: The interimSpace size between views.
*/ */
public init(origin: CGPoint, interimSpace: InterimSpace = 16) { public convenience init(interimSpace: InterimSpace = 16) {
self.origin = origin self.init()
self.interimSpace = interimSpace self.interimSpace = interimSpace
} }
/// Convenience initializer.
public convenience init() {
self.init(origin: .zero)
}
/// Reload the view layout. /// Reload the view layout.
public func reload() { open func reload() {
isOpened = false isOpened = false
layoutButtons() layoutButtons()
} }
...@@ -117,7 +119,7 @@ public class Menu { ...@@ -117,7 +119,7 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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) { 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 { if isEnabled {
disable() disable()
switch direction { switch direction {
...@@ -143,7 +145,7 @@ public class Menu { ...@@ -143,7 +145,7 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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) { 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 { if isEnabled {
disable() disable()
switch direction { switch direction {
...@@ -170,35 +172,40 @@ public class Menu { ...@@ -170,35 +172,40 @@ public class Menu {
- Parameter completion: A completion 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)?) { private func openUpAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views { guard let base = views.first else {
var base: UIView? return
for i in 1..<v.count {
if nil == base {
base = v[0]
} }
let view: UIView = v[i]
view.isHidden = false for i in 1..<views.count {
let v = views[i]
v.isHidden = false
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self, base = base] in
if let s: Menu = self { guard let s = self else {
view.alpha = 1 return
view.y = base!.y - CGFloat(i) * s.itemSize.height - CGFloat(i) * s.interimSpace
animations?(view)
} }
v.alpha = 1
v.y = base.y - CGFloat(i) * s.itemSize.height - CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
s.enable(view: view) return
if view == v.last {
s.isOpened = true
}
completion?(view)
} }
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
} }
completion?(v)
} }
} }
} }
...@@ -213,10 +220,10 @@ public class Menu { ...@@ -213,10 +220,10 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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)?) { private 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 { for i in 1..<views.count {
let view: UIView = v[i] let v = views[i]
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
...@@ -224,21 +231,27 @@ public class Menu { ...@@ -224,21 +231,27 @@ public class Menu {
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self] in
if let s: Menu = self { guard let s = self else {
view.alpha = 0 return
view.y = s.origin.y
animations?(view)
} }
v.alpha = 0
v.y = s.y
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
view.isHidden = true return
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
} }
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
} }
completion?(v)
} }
} }
} }
...@@ -254,37 +267,41 @@ public class Menu { ...@@ -254,37 +267,41 @@ public class Menu {
- Parameter completion: A completion 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)?) { private func openDownAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views { guard let base = views.first else {
var base: UIView? return
for i in 1..<v.count {
if nil == base {
base = v[0]
} }
let view: UIView = v[i] for i in 1..<views.count {
view.isHidden = false let v = views[i]
v.isHidden = false
let h: CGFloat = nil == baseSize ? itemSize.height : baseSize!.height let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self, base = base] in
if let s: Menu = self { guard let s = self else {
view.alpha = 1 return
view.y = base!.y + h + CGFloat(i - 1) * s.itemSize.height + CGFloat(i) * s.interimSpace
animations?(view)
} }
v.alpha = 1
v.y = base.y + h + CGFloat(i - 1) * s.itemSize.height + CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
s.enable(view: view) return
if view == v.last {
s.isOpened = true
}
completion?(view)
} }
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
} }
completion?(v)
} }
} }
} }
...@@ -299,33 +316,38 @@ public class Menu { ...@@ -299,33 +316,38 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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)?) { private 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..<views.count {
for i in 1..<v.count { let v = views[i]
let view: UIView = v[i]
let h: CGFloat = nil == baseSize ? itemSize.height : baseSize!.height let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self] in
if let s: Menu = self { guard let s = self else {
view.alpha = 0 return
view.y = s.origin.y + h
animations?(view)
} }
v.alpha = 0
v.y = s.y + h
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
view.isHidden = true return
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
} }
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
} }
completion?(v)
} }
} }
} }
...@@ -341,36 +363,40 @@ public class Menu { ...@@ -341,36 +363,40 @@ public class Menu {
- Parameter completion: A completion 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)?) { private func openLeftAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views { guard let base = views.first else {
var base: UIView? return
for i in 1..<v.count {
if nil == base {
base = v[0]
} }
let view: UIView = v[i] for i in 1..<views.count {
view.isHidden = false let v = views[i]
v.isHidden = false
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self, base = base] in
if let s: Menu = self { guard let s = self else {
view.alpha = 1 return
view.x = base!.x - CGFloat(i) * s.itemSize.width - CGFloat(i) * s.interimSpace
animations?(view)
} }
v.alpha = 1
v.x = base.x - CGFloat(i) * s.itemSize.width - CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
s.enable(view: view) return
if view == v.last {
s.isOpened = true
}
completion?(view)
} }
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
} }
completion?(v)
} }
} }
} }
...@@ -385,31 +411,37 @@ public class Menu { ...@@ -385,31 +411,37 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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)?) { private 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..<views.count {
for i in 1..<v.count { let v = views[i]
let view: UIView = v[i]
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self] in
if let s: Menu = self { guard let s = self else {
view.alpha = 0 return
view.x = s.origin.x
animations?(view)
} }
v.alpha = 0
v.x = s.x
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
view.isHidden = true return
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
} }
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
} }
completion?(v)
} }
} }
} }
...@@ -425,36 +457,41 @@ public class Menu { ...@@ -425,36 +457,41 @@ public class Menu {
- Parameter completion: A completion 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)?) { private func openRightAnimation(duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions, animations: ((UIView) -> Void)?, completion: ((UIView) -> Void)?) {
if let v = views { guard let base = views.first else {
var base: UIView? return
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 for i in 1..<views.count {
let v = views[i]
v.isHidden = false
let h = baseSize.height
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self, base = base] in
if let s: Menu = self { guard let s = self else {
view.alpha = 1 return
view.x = base!.x + h + CGFloat(i - 1) * s.itemSize.width + CGFloat(i) * s.interimSpace
animations?(view)
} }
v.alpha = 1
v.x = base.x + h + CGFloat(i - 1) * s.itemSize.width + CGFloat(i) * s.interimSpace
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
s.enable(view: view) return
if view == v.last {
s.isOpened = true
}
completion?(view)
} }
s.enable(view: v)
if v == s.views.last {
s.isOpened = true
} }
completion?(v)
} }
} }
} }
...@@ -469,66 +506,66 @@ public class Menu { ...@@ -469,66 +506,66 @@ public class Menu {
- Parameter animations: An animation block to execute on each view's 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. - 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)?) { private 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..<views.count {
for i in 1..<v.count { let v = views[i]
let view: UIView = v[i] let w = baseSize.width
let w: CGFloat = nil == baseSize ? itemSize.width : baseSize!.width
UIView.animate(withDuration: Double(i) * duration, UIView.animate(withDuration: Double(i) * duration,
delay: delay, delay: delay,
usingSpringWithDamping: usingSpringWithDamping, usingSpringWithDamping: usingSpringWithDamping,
initialSpringVelocity: initialSpringVelocity, initialSpringVelocity: initialSpringVelocity,
options: options, options: options,
animations: { [weak self] in animations: { [weak self] in
if let s: Menu = self { guard let s = self else {
view.alpha = 0 return
view.x = s.origin.x + w
animations?(view)
} }
v.alpha = 0
v.x = s.x + w
animations?(v)
}) { [weak self] _ in }) { [weak self] _ in
if let s: Menu = self { guard let s = self else {
view.isHidden = true return
s.enable(view: view)
if view == v.last {
s.isOpened = false
}
completion?(view)
} }
v.isHidden = true
s.enable(view: v)
if v == s.views.last {
s.isOpened = false
} }
completion?(v)
} }
} }
} }
/// Layout the views. /// Layout the views.
private func layoutButtons() { private func layoutButtons() {
if let v = views { for i in 0..<views.count {
let size: CGSize = nil == baseSize ? itemSize : baseSize! let v = views[i]
for i in 0..<v.count {
let view: UIView = v[i]
if 0 == i { if 0 == i {
view.frame.size = size v.frame.size = baseSize
view.frame.origin = origin v.layer.zPosition = 10000
view.layer.zPosition = 10000
} else { } else {
view.alpha = 0 v.alpha = 0
view.isHidden = true v.isHidden = true
view.frame.size = itemSize v.frame.size = itemSize
view.x = origin.x + (size.width - itemSize.width) / 2 v.x = x + (baseSize.width - itemSize.width) / 2
view.y = origin.y + (size.height - itemSize.height) / 2 v.y = y + (baseSize.height - itemSize.height) / 2
view.layer.zPosition = CGFloat(10000 - v.count - i) v.layer.zPosition = CGFloat(10000 - views.count - i)
}
} }
} }
} }
/// Disable the Menu if views exist. /// Disable the Menu if views exist.
private func disable() { private func disable() {
if let v = views { guard 0 < views.count else {
if 0 < v.count { return
isEnabled = false
}
} }
isEnabled = false
} }
/** /**
...@@ -536,10 +573,9 @@ public class Menu { ...@@ -536,10 +573,9 @@ public class Menu {
- Parameter view: UIView that is passed in to compare. - Parameter view: UIView that is passed in to compare.
*/ */
private func enable(view: UIView) { private func enable(view: UIView) {
if let v = views { guard view == views.last else {
if view == v.last { return
isEnabled = true
}
} }
isEnabled = true
} }
} }
...@@ -49,35 +49,49 @@ extension UIViewController { ...@@ -49,35 +49,49 @@ extension UIViewController {
} }
@IBDesignable @IBDesignable
public class MenuController: RootController { open class MenuController: RootController {
/// Reference to the MenuView. /// 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. Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when - Parameter completion: An Optional callback that is executed when
all menu items have been opened. all menu items have been opened.
*/ */
public func openMenu(completion: (() -> Void)? = nil) { open func open(completion: (() -> Void)? = nil) {
if true == isUserInteractionEnabled { if true == isUserInteractionEnabled {
isUserInteractionEnabled = false isUserInteractionEnabled = false
rootViewController.view.alpha = 0.5 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. Opens the menu with a callback.
- Parameter completion: An Optional callback that is executed when - Parameter completion: An Optional callback that is executed when
all menu items have been closed. all menu items have been closed.
*/ */
public func closeMenu(completion: (() -> Void)? = nil) { open func close(completion: (() -> Void)? = nil) {
if false == isUserInteractionEnabled { if false == isUserInteractionEnabled {
rootViewController.view.alpha = 1 rootViewController.view.alpha = 1
menuView.close(completion: { [weak self] in menu.open { [weak self] (view) in
self?.isUserInteractionEnabled = true guard let s = self else {
return
}
if view == s.menu.views.last {
s.isUserInteractionEnabled = true
completion?() completion?()
}) }
}
} }
} }
...@@ -86,7 +100,7 @@ public class MenuController: RootController { ...@@ -86,7 +100,7 @@ public class MenuController: RootController {
method. LayoutSubviews should be called immediately, unless you method. LayoutSubviews should be called immediately, unless you
have a certain need. have a certain need.
*/ */
public override func layoutSubviews() { open override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
rootViewController.view.frame = view.bounds rootViewController.view.frame = view.bounds
} }
...@@ -98,14 +112,14 @@ public class MenuController: RootController { ...@@ -98,14 +112,14 @@ public class MenuController: RootController {
The super.prepareView method should always be called immediately The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
public override func prepareView() { open override func prepareView() {
super.prepareView() super.prepareView()
prepareMenuView() prepareMenuView()
} }
/// Prepares the MenuView. /// Prepares the MenuView.
private func prepareMenuView() { private func prepareMenuView() {
menuView.zPosition = 1000 menu.zPosition = 1000
view.addSubview(menuView) 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 @@ ...@@ -30,7 +30,7 @@
import UIKit import UIKit
open class PhotoLibraryController: UIViewController, PhotoLibraryDelegate { open class PhotoLibraryController: UIViewController {
/// A reference to a PhotoLibrary. /// A reference to a PhotoLibrary.
public private(set) lazy var photoLibrary: PhotoLibrary = PhotoLibrary() public private(set) lazy var photoLibrary: PhotoLibrary = PhotoLibrary()
...@@ -58,3 +58,5 @@ open class PhotoLibraryController: UIViewController, PhotoLibraryDelegate { ...@@ -58,3 +58,5 @@ open class PhotoLibraryController: UIViewController, PhotoLibraryDelegate {
photoLibrary.delegate = self 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