Commit 31330fc6 by Daniel Dahan

working copy of TabsController using Motion transitions

parent ffbc3884
......@@ -97,7 +97,6 @@
965E811F1DD4D5C800D61E4B /* ErrorTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; };
965E81211DD4D5C800D61E4B /* TextStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB79D1CB40DC500C806FE /* TextStorage.swift */; };
965E81221DD4D5C800D61E4B /* TextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB79E1CB40DC500C806FE /* TextView.swift */; };
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; };
965E81261DD4D7C800D61E4B /* CharacterAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961276621DCD8B1800A7D920 /* CharacterAttribute.swift */; };
966995491F1433C30064E7E4 /* PageTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966995481F1433C30064E7E4 /* PageTabBarController.swift */; };
966C17731F0439F600D3E83C /* Material+MotionAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 966C17721F0439F600D3E83C /* Material+MotionAnimation.swift */; };
......@@ -143,7 +142,6 @@
96BCB83C1CB4115200C806FE /* Bar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7981CB40DC500C806FE /* Bar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB83D1CB4115200C806FE /* RootController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7991CB40DC500C806FE /* RootController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8441CB4115200C806FE /* BottomNavigationController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7581CB40DC500C806FE /* BottomNavigationController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8451CB4115200C806FE /* BottomTabBar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7591CB40DC500C806FE /* BottomTabBar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8461CB4115200C806FE /* NavigationBar.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7901CB40DC500C806FE /* NavigationBar.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8471CB4115200C806FE /* NavigationController.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7911CB40DC500C806FE /* NavigationController.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8481CB4115200C806FE /* NavigationItem.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB7921CB40DC500C806FE /* NavigationItem.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -240,7 +238,6 @@
96A183621E0C6CE200083C30 /* FABMenu.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABMenu.swift; sourceTree = "<group>"; };
96A183641E0C6DD400083C30 /* FABMenuController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABMenuController.swift; sourceTree = "<group>"; };
96BCB7581CB40DC500C806FE /* BottomNavigationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomNavigationController.swift; sourceTree = "<group>"; };
96BCB7591CB40DC500C806FE /* BottomTabBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomTabBar.swift; sourceTree = "<group>"; };
96BCB75D1CB40DC500C806FE /* Card.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Card.swift; sourceTree = "<group>"; };
96BCB75F1CB40DC500C806FE /* FABButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FABButton.swift; sourceTree = "<group>"; };
96BCB7601CB40DC500C806FE /* FlatButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlatButton.swift; sourceTree = "<group>"; };
......@@ -431,7 +428,6 @@
962DDD081D6FBBD0001C307C /* BottomTabBar */ = {
isa = PBXGroup;
children = (
96BCB7591CB40DC500C806FE /* BottomTabBar.swift */,
96BCB7581CB40DC500C806FE /* BottomNavigationController.swift */,
);
name = BottomTabBar;
......@@ -878,7 +874,6 @@
96BCB83C1CB4115200C806FE /* Bar.swift in Headers */,
96BCB83D1CB4115200C806FE /* RootController.swift in Headers */,
96BCB8441CB4115200C806FE /* BottomNavigationController.swift in Headers */,
96BCB8451CB4115200C806FE /* BottomTabBar.swift in Headers */,
96BCB8461CB4115200C806FE /* NavigationBar.swift in Headers */,
96BCB8471CB4115200C806FE /* NavigationController.swift in Headers */,
96BCB8481CB4115200C806FE /* NavigationItem.swift in Headers */,
......@@ -1015,7 +1010,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
965E81231DD4D7C800D61E4B /* BottomTabBar.swift in Sources */,
96DE3CE91F269BF00035F7AF /* MotionPlugin.swift in Sources */,
961E6BE21DDA2AF3004E6C93 /* Screen.swift in Sources */,
96DE3CF01F269BF00035F7AF /* IgnoreSubviewModifiersPreprocessor.swift in Sources */,
......
......@@ -30,6 +30,24 @@
import UIKit
extension UIViewController {
/**
A convenience property that provides access to the BottomNavigationController.
This is the recommended method of accessing the BottomNavigationController
through child UIViewControllers.
*/
public var bottomNavigationController: BottomNavigationController? {
var viewController: UIViewController? = self
while nil != viewController {
if viewController is BottomNavigationController {
return viewController as? BottomNavigationController
}
viewController = viewController?.parent
}
return nil
}
}
open class BottomNavigationController: UITabBarController {
/**
An initializer that initializes the object with a NSCoder object.
......@@ -112,17 +130,21 @@ open class BottomNavigationController: UITabBarController {
when subclassing.
*/
open func prepare() {
view.backgroundColor = .white
view.clipsToBounds = true
view.contentScaleFactor = Screen.scale
view.backgroundColor = .white
prepareTabBar()
}
}
extension BottomNavigationController {
/// Prepares the tabBar.
private func prepareTabBar() {
fileprivate func prepareTabBar() {
tabBar.isTranslucent = false
tabBar.heightPreset = .normal
tabBar.depthPreset = .depth1
tabBar.dividerAlignment = .top
let image = UIImage()
tabBar.shadowImage = image
tabBar.backgroundImage = image
......
/*
* Copyright (C) 2015 - 2017, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>.
* 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
extension UITabBarItem {
/// Sets the color of the title color for a state.
public func setTitleColor(color: UIColor, forState state: UIControlState) {
setTitleTextAttributes([NSForegroundColorAttributeName: color], for: state)
}
}
open class BottomTabBar: UITabBar {
open override var intrinsicContentSize: CGSize {
return CGSize(width: width, height: height)
}
/// Automatically aligns the BottomNavigationBar to the superview.
@IBInspectable
open var isAlignedToParentAutomatically = true
/// A property that accesses the backing layer's background
@IBInspectable
open override var backgroundColor: UIColor? {
didSet {
barTintColor = backgroundColor
}
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
*/
public override init(frame: CGRect) {
super.init(frame: frame)
prepare()
}
/// A convenience initializer.
public convenience init() {
self.init(frame: .zero)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
open override func layoutSubviews() {
super.layoutSubviews()
layoutShape()
layoutShadowPath()
if let v = items {
for item in v {
if .phone == Device.userInterfaceIdiom {
if nil == item.title {
let inset: CGFloat = 7
item.imageInsets = UIEdgeInsetsMake(inset, 0, -inset, 0)
} else {
let inset: CGFloat = 6
item.titlePositionAdjustment.vertical = -inset
}
} else if nil == item.title {
let inset: CGFloat = 9
item.imageInsets = UIEdgeInsetsMake(inset, 0, -inset, 0)
} else {
let inset: CGFloat = 3
item.imageInsets = UIEdgeInsetsMake(inset, 0, -inset, 0)
item.titlePositionAdjustment.vertical = -inset
}
}
}
layoutDivider()
}
open override func didMoveToSuperview() {
super.didMoveToSuperview()
if isAlignedToParentAutomatically {
if let v = superview {
v.layout(self).bottom().horizontally()
}
}
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
public func prepare() {
heightPreset = .normal
depthPreset = .depth1
dividerAlignment = .top
contentScaleFactor = Screen.scale
backgroundColor = .white
let image = UIImage.image(with: .clear, size: CGSize(width: 1, height: 1))
shadowImage = image
backgroundImage = image
}
}
......@@ -296,6 +296,7 @@ open class NavigationBar: UINavigationBar {
contentEdgeInsetsPreset = .square1
contentScaleFactor = Screen.scale
backButtonImage = Icon.cm.arrowBack
let image = UIImage()
shadowImage = image
setBackgroundImage(image, for: .default)
......
......@@ -219,7 +219,7 @@ open class PageTabBarController: RootController {
open override func prepare() {
super.prepare()
preparePageTabBar()
preparePageTabBarItems()
prepareTabItems()
}
open override func prepareRootViewController() {
......@@ -237,7 +237,7 @@ open class PageTabBarController: RootController {
}
/// Prepares the pageTabBarItems.
open func preparePageTabBarItems() {
open func prepareTabItems() {
pageTabBar.buttons.removeAll()
for x in viewControllers {
......
......@@ -30,7 +30,6 @@
import UIKit
/// A memory reference to the TabItem instance for UIViewController extensions.
fileprivate var TabItemKey: UInt8 = 0
open class TabItem: FlatButton {
......@@ -80,27 +79,12 @@ extension UIViewController {
}
open class TabsController: UIViewController {
/// A reference to the currently selected view controller index value.
@IBInspectable
open var selectedIndex = 0
/// Enables and disables bouncing when swiping.
open var isBounceEnabled: Bool {
get {
return scrollView.bounces
}
set(value) {
scrollView.bounces = value
}
}
/// The TabBar used to switch between view controllers.
@IBInspectable
open fileprivate(set) var tabBar: TabBar?
/// The UIScrollView used to pan the application pages.
@IBInspectable
open let scrollView = UIScrollView()
public let container = UIView()
/// An Array of UIViewControllers.
open var viewControllers: [UIViewController] {
......@@ -109,18 +93,26 @@ open class TabsController: UIViewController {
self?.removeViewController(viewController: $0)
}
prepareViewControllers()
prepareTabBar()
prepareContainer()
layoutSubviews()
}
}
/// A reference to the currently selected view controller index value.
@IBInspectable
open var selectedIndex = 0
/// The tabBar alignment.
open var tabBarAlignment = TabBarAlignment.bottom {
didSet {
layoutSubviews()
}
}
/// The transition type used during a transition.
open var transitionType = MotionTransitionType.autoReverse(presenting: .fade)
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
......@@ -130,16 +122,6 @@ open class TabsController: UIViewController {
super.init(coder: aDecoder)
}
/// Previous scroll view content offset.
fileprivate var previousContentOffset: CGFloat = 0
/// The number of views used in the scrollViewPool.
open var viewPoolCount = 3 {
didSet {
layoutSubviews()
}
}
/**
An initializer that accepts an Array of UIViewControllers.
- Parameter viewControllers: An Array of UIViewControllers.
......@@ -167,8 +149,9 @@ open class TabsController: UIViewController {
*/
open func layoutSubviews() {
layoutTabBar()
layoutScrollView()
layoutViewControllers()
layoutContainer()
viewControllers[selectedIndex].view.frame.size = container.bounds.size
}
/**
......@@ -180,52 +163,16 @@ open class TabsController: UIViewController {
*/
open func prepare() {
view.contentScaleFactor = Screen.scale
prepareScrollView()
prepareViewControllers()
prepareContainer()
prepareTabBar()
}
}
extension TabsController {
/// Prepares the scrollView used to pan through view controllers.
fileprivate func prepareScrollView() {
scrollView.delegate = self
scrollView.bounces = false
scrollView.isPagingEnabled = true
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.contentScaleFactor = Screen.scale
view.addSubview(scrollView)
}
/// Prepares the view controllers.
fileprivate func prepareViewControllers() {
let n = viewControllers.count
guard 1 < n else {
if 1 == n {
prepareViewController(at: 0)
}
return
}
let m = viewPoolCount < n ? viewPoolCount : n
if 0 == selectedIndex {
for i in 0..<m {
for i in 0..<viewControllers.count {
prepareViewController(at: i)
}
} else if n - 1 == selectedIndex {
for i in 0..<m {
prepareViewController(at: selectedIndex - i)
}
} else {
prepareViewController(at: selectedIndex)
prepareViewController(at: selectedIndex - 1)
prepareViewController(at: selectedIndex + 1)
}
}
}
extension TabsController {
/**
Prepares the tabBar buttons.
- Parameter _ buttons: An Array of UIButtons.
......@@ -243,6 +190,7 @@ extension TabsController {
}
}
/// Prepares the TabBar.
fileprivate func prepareTabBar() {
var buttons = [UIButton]()
......@@ -271,6 +219,11 @@ extension TabsController {
prepareTabBarButtons(buttons)
}
/// Prepares the container view.
fileprivate func prepareContainer() {
view.addSubview(container)
}
/**
Loads a view controller based on its index in the viewControllers Array
and adds it as a child view controller.
......@@ -287,30 +240,15 @@ extension TabsController {
vc.didMove(toParentViewController: self)
vc.view.clipsToBounds = true
vc.view.contentScaleFactor = Screen.scale
scrollView.addSubview(vc.view)
}
/**
Transitions from one view controller to another.
- Parameter from: The index of the view controller to transition from.
- Parameter to: The index of the view controller to transition to.
*/
fileprivate func prepareViewControllersForTransition(from: Int, to: Int) {
let fromVC = viewControllers[from]
let toVC = viewControllers[to]
fromVC.present(toVC, animated: true)
container.addSubview(vc.view)
}
}
extension TabsController {
fileprivate func layoutScrollView() {
let w = view.bounds.width
scrollView.contentSize = CGSize(width: w * CGFloat(viewControllers.count), height: scrollView.height)
scrollView.contentOffset = CGPoint(x: w * CGFloat(selectedIndex), y: 0)
/// Layout the container view.
fileprivate func layoutContainer() {
guard let v = tabBar else {
scrollView.frame = view.bounds
container.frame = view.bounds
return
}
......@@ -319,58 +257,20 @@ extension TabsController {
switch tabBarAlignment {
case .top:
scrollView.y = p
scrollView.height = y
container.y = p
container.height = y
case .bottom:
scrollView.y = 0
scrollView.height = y
container.y = 0
container.height = y
case .hidden:
scrollView.y = 0
scrollView.height = view.height
}
scrollView.width = w
container.y = 0
container.height = view.height
}
fileprivate func layoutViewControllers() {
let n = viewControllers.count
guard 1 < n else {
layoutViewController(at: 0, position: 0)
return
container.width = view.bounds.width
}
let m = viewPoolCount < n ? viewPoolCount : n
if 0 == selectedIndex {
for i in 0..<m {
layoutViewController(at: i, position: i)
}
} else if n - 1 == selectedIndex {
var q = 0
for i in 0..<m {
q = selectedIndex - i
layoutViewController(at: q, position: q)
}
} else {
layoutViewController(at: selectedIndex, position: selectedIndex)
layoutViewController(at: selectedIndex - 1, position: selectedIndex - 1)
layoutViewController(at: selectedIndex + 1, position: selectedIndex + 1)
}
}
/**
Positions a view controller within the scrollView.
- Parameter position: An Int for the position of the view controller.
*/
fileprivate func layoutViewController(at index: Int, position: Int) {
let w = scrollView.width
viewControllers[index].view.frame = CGRect(x: CGFloat(position) * w, y: 0, width: w, height: scrollView.height)
}
/**
Layout the TabBar.
*/
/// Layout the TabBar.
fileprivate func layoutTabBar() {
guard let v = tabBar else {
return
......@@ -394,37 +294,6 @@ extension TabsController {
}
extension TabsController {
/// Removes the view controllers not within the scrollView.
fileprivate func removeViewControllers() {
let n = viewControllers.count
guard 1 < n else {
return
}
if 0 == selectedIndex {
for i in 1..<n {
removeViewController(at: i)
}
} else if n - 1 == selectedIndex {
for i in 0..<n - 2 {
removeViewController(at: i)
}
} else {
for i in 0..<selectedIndex {
removeViewController(at: i)
}
let x = selectedIndex + 1
if x < n {
for i in x..<n {
removeViewController(at: i)
}
}
}
}
/**
Removes the view controller as a child view controller with
the given index.
......@@ -469,33 +338,15 @@ extension TabsController {
guard i != selectedIndex else {
return
}
// removeViewControllers()
// prepareViewControllersForTransition(from: selectedIndex, to: i)
let fvc = viewControllers[selectedIndex]
let tvc = viewControllers[i]
tvc.view.frame.size = container.bounds.size
tvc.motionModalTransitionType = transitionType
Motion.shared.transition(from: fvc, to: tvc, in: container)
selectedIndex = i
v.select(at: i)
removeViewControllers()
prepareViewControllers()
prepareTabBar()
layoutSubviews()
}
}
extension TabsController: UIScrollViewDelegate {
@objc
open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let w = scrollView.width
let p = Int(floor((scrollView.contentOffset.x - w / 2) / w) + 1)
selectedIndex = p
removeViewControllers()
prepareViewControllers()
// prepareTabBar()
layoutSubviews()
}
}
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