From 76e60ab662fb009637c3e7dfc3f004364045f399 Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Wed, 12 Sep 2018 01:39:13 +0400 Subject: [PATCH 1/5] Added direction to DepthPreset --- Sources/iOS/Depth.swift | 77 +++++++++++++++++++++++++++++- Sources/iOS/Material+CALayer.swift | 2 +- Sources/iOS/Material+UIView.swift | 1 - 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/Sources/iOS/Depth.swift b/Sources/iOS/Depth.swift index 8dd76619c..fb69e901c 100644 --- a/Sources/iOS/Depth.swift +++ b/Sources/iOS/Depth.swift @@ -30,14 +30,32 @@ import UIKit -@objc(DepthPreset) -public enum DepthPreset: Int { +public enum DepthPreset { case none case depth1 case depth2 case depth3 case depth4 case depth5 + indirect case above(DepthPreset) + indirect case below(DepthPreset) + indirect case left(DepthPreset) + indirect case right(DepthPreset) + + public var root: DepthPreset { + switch self { + case .above(let v): + return v.root + case .below(let v): + return v.root + case .left(let v): + return v.root + case .right(let v): + return v.root + default: + return self + } + } } public struct Depth { @@ -110,5 +128,60 @@ public func DepthPresetToValue(preset: DepthPreset) -> Depth { return Depth(offset: Offset(horizontal: 0, vertical: 4), opacity: 0.3, radius: 4) case .depth5: return Depth(offset: Offset(horizontal: 0, vertical: 8), opacity: 0.3, radius: 8) + case .above(let preset): + var v = DepthPresetToValue(preset: preset) + if preset.isRoot { + v.offset.vertical *= -1 + } else { + let root = DepthPresetToValue(preset: preset.root) + v.offset.vertical -= root.offset.vertical + } + return v + case .below(let preset): + var v = DepthPresetToValue(preset: preset) + if preset.isRoot { + return v + } else { + let root = DepthPresetToValue(preset: preset.root) + v.offset.vertical += root.offset.vertical + } + return v + case .left(let preset): + var v = DepthPresetToValue(preset: preset) + if preset.isRoot { + v.offset.horizontal = -v.offset.vertical + v.offset.vertical = 0 + } else { + let root = DepthPresetToValue(preset: preset.root) + v.offset.horizontal -= root.offset.vertical + } + return v + case .right(let preset): + var v = DepthPresetToValue(preset: preset) + if preset.isRoot { + v.offset.horizontal = v.offset.vertical + v.offset.vertical = 0 + } else { + let root = DepthPresetToValue(preset: preset.root) + v.offset.horizontal += root.offset.vertical + } + return v + } +} + +fileprivate extension DepthPreset { + var isRoot: Bool { + switch self { + case .above(_): + return false + case .below(_): + return false + case .left(_): + return false + case .right(_): + return false + default: + return true + } } } diff --git a/Sources/iOS/Material+CALayer.swift b/Sources/iOS/Material+CALayer.swift index a7bc50571..e3598717a 100644 --- a/Sources/iOS/Material+CALayer.swift +++ b/Sources/iOS/Material+CALayer.swift @@ -281,7 +281,7 @@ extension CALayer { return } - if .none == depthPreset { + if case .none = depthPreset.root { shadowPath = nil } else if nil == shadowPath { shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath diff --git a/Sources/iOS/Material+UIView.swift b/Sources/iOS/Material+UIView.swift index f4d035204..df99fb0ec 100644 --- a/Sources/iOS/Material+UIView.swift +++ b/Sources/iOS/Material+UIView.swift @@ -87,7 +87,6 @@ extension UIView { } /// A preset value for Depth. - @objc open var depthPreset: DepthPreset { get { return layer.depthPreset From 0a873b756088aa9c6ddcf101db0e30886ec73e7e Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Wed, 12 Sep 2018 04:20:51 +0400 Subject: [PATCH 2/5] Added support for heightPreset in BottomNavigationController --- Sources/iOS/BottomNavigationController.swift | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Sources/iOS/BottomNavigationController.swift b/Sources/iOS/BottomNavigationController.swift index 1924a979b..48958df13 100644 --- a/Sources/iOS/BottomNavigationController.swift +++ b/Sources/iOS/BottomNavigationController.swift @@ -41,6 +41,15 @@ extension UIViewController { } } +private class MaterialTabBar: UITabBar{ + override func sizeThatFits(_ size: CGSize) -> CGSize { + var v = super.sizeThatFits(size) + let offset = v.height - 49 + v.height = CGFloat(heightPreset.rawValue) + offset + return v + } +} + open class BottomNavigationController: UITabBarController { /// A Boolean that indicates if the swipe feature is enabled.. open var isSwipeEnabled = false { @@ -60,6 +69,7 @@ open class BottomNavigationController: UITabBarController { */ public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) + setTabBarClass() } /** @@ -69,6 +79,7 @@ open class BottomNavigationController: UITabBarController { */ public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) + setTabBarClass() } /// An initializer that accepts no parameters. @@ -143,6 +154,17 @@ open class BottomNavigationController: UITabBarController { } } +private extension BottomNavigationController { + /// Sets tabBar class to MaterialTabBar. + func setTabBarClass() { + guard object_getClass(tabBar) === UITabBar.self else { + return + } + + object_setClass(tabBar, MaterialTabBar.self) + } +} + private extension BottomNavigationController { /** Selects a view controller at a given index. From 10c309c02a6db9bd6e4aa0a021219661fe012800 Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Wed, 12 Sep 2018 04:37:23 +0400 Subject: [PATCH 3/5] Added .custom(x) case for HeightPreset --- Sources/iOS/HeightPreset.swift | 51 +++++++++++++++++++++++-------- Sources/iOS/Material+UIView.swift | 1 - 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Sources/iOS/HeightPreset.swift b/Sources/iOS/HeightPreset.swift index 6a3ae1fe2..4252d3c89 100644 --- a/Sources/iOS/HeightPreset.swift +++ b/Sources/iOS/HeightPreset.swift @@ -30,16 +30,43 @@ import UIKit -@objc(HeightPreset) -public enum HeightPreset: Int { - case none = 0 - case tiny = 20 - case xsmall = 28 - case small = 36 - case `default` = 44 - case normal = 49 - case medium = 52 - case large = 60 - case xlarge = 68 - case xxlarge = 104 +public enum HeightPreset { + case none + case tiny + case xsmall + case small + case `default` + case normal + case medium + case large + case xlarge + case xxlarge + case custom(CGFloat) + + public var rawValue: CGFloat { + switch self { + case .none: + return 0 + case .tiny: + return 20 + case .xsmall: + return 28 + case .small: + return 36 + case .`default`: + return 44 + case .normal: + return 49 + case .medium: + return 52 + case .large: + return 60 + case .xlarge: + return 68 + case .xxlarge: + return 104 + case .custom(let v): + return v + } + } } diff --git a/Sources/iOS/Material+UIView.swift b/Sources/iOS/Material+UIView.swift index df99fb0ec..edd0772fc 100644 --- a/Sources/iOS/Material+UIView.swift +++ b/Sources/iOS/Material+UIView.swift @@ -61,7 +61,6 @@ extension UIView { } /// HeightPreset value. - @objc open var heightPreset: HeightPreset { get { return layer.heightPreset From 5566ea5becb846aad3e012e2953ea0d41342b48f Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Wed, 12 Sep 2018 06:16:37 +0400 Subject: [PATCH 4/5] Minor clean up --- Sources/iOS/BottomNavigationController.swift | 6 +++--- Sources/iOS/Material+CALayer.swift | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/iOS/BottomNavigationController.swift b/Sources/iOS/BottomNavigationController.swift index 48958df13..b84dcf18f 100644 --- a/Sources/iOS/BottomNavigationController.swift +++ b/Sources/iOS/BottomNavigationController.swift @@ -41,11 +41,11 @@ extension UIViewController { } } -private class MaterialTabBar: UITabBar{ +private class MaterialTabBar: UITabBar { override func sizeThatFits(_ size: CGSize) -> CGSize { var v = super.sizeThatFits(size) - let offset = v.height - 49 - v.height = CGFloat(heightPreset.rawValue) + offset + let offset = v.height - HeightPreset.normal.rawValue + v.height = heightPreset.rawValue + offset return v } } diff --git a/Sources/iOS/Material+CALayer.swift b/Sources/iOS/Material+CALayer.swift index e3598717a..7cab822c6 100644 --- a/Sources/iOS/Material+CALayer.swift +++ b/Sources/iOS/Material+CALayer.swift @@ -38,7 +38,7 @@ fileprivate class MaterialLayer { /// A property that sets the height of the layer's frame. fileprivate var heightPreset = HeightPreset.default { didSet { - layer?.height = CGFloat(heightPreset.rawValue) + layer?.height = heightPreset.rawValue } } From 9707a8f60d95c1afec46a91f0a859398f843f99e Mon Sep 17 00:00:00 2001 From: Orkhan Alikhanov Date: Wed, 12 Sep 2018 17:01:03 +0400 Subject: [PATCH 5/5] Renamed DepthPreset.root to rawValue --- Sources/iOS/Depth.swift | 28 +++++++++++++++------------- Sources/iOS/Material+CALayer.swift | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Sources/iOS/Depth.swift b/Sources/iOS/Depth.swift index fb69e901c..cbee268ac 100644 --- a/Sources/iOS/Depth.swift +++ b/Sources/iOS/Depth.swift @@ -42,16 +42,17 @@ public enum DepthPreset { indirect case left(DepthPreset) indirect case right(DepthPreset) - public var root: DepthPreset { + /// Returns raw depth value without considering direction. + public var rawValue: DepthPreset { switch self { case .above(let v): - return v.root + return v.rawValue case .below(let v): - return v.root + return v.rawValue case .left(let v): - return v.root + return v.rawValue case .right(let v): - return v.root + return v.rawValue default: return self } @@ -133,8 +134,8 @@ public func DepthPresetToValue(preset: DepthPreset) -> Depth { if preset.isRoot { v.offset.vertical *= -1 } else { - let root = DepthPresetToValue(preset: preset.root) - v.offset.vertical -= root.offset.vertical + let value = DepthPresetToValue(preset: preset.rawValue) + v.offset.vertical -= value.offset.vertical } return v case .below(let preset): @@ -142,8 +143,8 @@ public func DepthPresetToValue(preset: DepthPreset) -> Depth { if preset.isRoot { return v } else { - let root = DepthPresetToValue(preset: preset.root) - v.offset.vertical += root.offset.vertical + let value = DepthPresetToValue(preset: preset.rawValue) + v.offset.vertical += value.offset.vertical } return v case .left(let preset): @@ -152,8 +153,8 @@ public func DepthPresetToValue(preset: DepthPreset) -> Depth { v.offset.horizontal = -v.offset.vertical v.offset.vertical = 0 } else { - let root = DepthPresetToValue(preset: preset.root) - v.offset.horizontal -= root.offset.vertical + let value = DepthPresetToValue(preset: preset.rawValue) + v.offset.horizontal -= value.offset.vertical } return v case .right(let preset): @@ -162,14 +163,15 @@ public func DepthPresetToValue(preset: DepthPreset) -> Depth { v.offset.horizontal = v.offset.vertical v.offset.vertical = 0 } else { - let root = DepthPresetToValue(preset: preset.root) - v.offset.horizontal += root.offset.vertical + let value = DepthPresetToValue(preset: preset.rawValue) + v.offset.horizontal += value.offset.vertical } return v } } fileprivate extension DepthPreset { + /// Checks if the preset is the root value (has no direction). var isRoot: Bool { switch self { case .above(_): diff --git a/Sources/iOS/Material+CALayer.swift b/Sources/iOS/Material+CALayer.swift index 7cab822c6..7f93a8a9f 100644 --- a/Sources/iOS/Material+CALayer.swift +++ b/Sources/iOS/Material+CALayer.swift @@ -281,7 +281,7 @@ extension CALayer { return } - if case .none = depthPreset.root { + if case .none = depthPreset.rawValue { shadowPath = nil } else if nil == shadowPath { shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath