Skip to content

Commit

Permalink
Optimize code and support rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yue Cai authored and Yue Cai committed Sep 26, 2017
1 parent d89c545 commit 0948878
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
5 changes: 1 addition & 4 deletions Tiptoes/Tiptoes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
674123551E279E47007E4393 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674123541E279E47007E4393 /* AppDelegate.swift */; };
674123571E279E47007E4393 /* TopMostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674123561E279E47007E4393 /* TopMostViewController.swift */; };
6741235C1E279E47007E4393 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6741235B1E279E47007E4393 /* Assets.xcassets */; };
6741235F1E279E47007E4393 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6741235D1E279E47007E4393 /* LaunchScreen.storyboard */; };
674123671E279EBC007E4393 /* TiptoesNavController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674123661E279EBC007E4393 /* TiptoesNavController.swift */; };
674123691E27A01F007E4393 /* BelowBackViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674123681E27A01F007E4393 /* BelowBackViewController.swift */; };
Expand All @@ -19,7 +18,6 @@
674123511E279E47007E4393 /* Tiptoes.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Tiptoes.app; sourceTree = BUILT_PRODUCTS_DIR; };
674123541E279E47007E4393 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
674123561E279E47007E4393 /* TopMostViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TopMostViewController.swift; sourceTree = "<group>"; };
6741235B1E279E47007E4393 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6741235E1E279E47007E4393 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
674123601E279E47007E4393 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
674123661E279EBC007E4393 /* TiptoesNavController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TiptoesNavController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -60,7 +58,6 @@
674123561E279E47007E4393 /* TopMostViewController.swift */,
674123681E27A01F007E4393 /* BelowBackViewController.swift */,
674123661E279EBC007E4393 /* TiptoesNavController.swift */,
6741235B1E279E47007E4393 /* Assets.xcassets */,
6741235D1E279E47007E4393 /* LaunchScreen.storyboard */,
674123601E279E47007E4393 /* Info.plist */,
);
Expand Down Expand Up @@ -128,7 +125,6 @@
buildActionMask = 2147483647;
files = (
6741235F1E279E47007E4393 /* LaunchScreen.storyboard in Resources */,
6741235C1E279E47007E4393 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -298,6 +294,7 @@
674123651E279E47007E4393 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
9 changes: 1 addition & 8 deletions Tiptoes/Tiptoes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var navigationController: TiptoesNavController!
var homeViewController: BelowBackViewController!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
homeViewController = BelowBackViewController()
navigationController = TiptoesNavController(rootViewController: homeViewController)
window?.rootViewController = navigationController
window?.rootViewController = TiptoesNavController(rootViewController: BelowBackViewController())
window?.makeKeyAndVisible()

UIApplication.shared.statusBarStyle = .lightContent

return true
}

Expand Down
25 changes: 16 additions & 9 deletions Tiptoes/Tiptoes/BelowBackViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ import UIKit

class BelowBackViewController: UIViewController {

lazy var demoButton: UIButton = {
let b = UIButton()
b.setTitle("JUMP TO NEXT", for: .normal)
b.setTitleColor(UIColor.black, for: .normal)
b.addTarget(self, action: #selector(nav), for: .touchUpInside)
return b
}()

override func viewDidLoad() {
super.viewDidLoad()

title = "HOME"
view.backgroundColor = UIColor(red: 59.0/255.0, green: 62.0/255.0, blue: 67.0/255.0, alpha: 1.0)
let button = UIButton()
button.setTitle("Button", for: .normal)
button.sizeToFit()
button.center = view.center
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(nav), for: .touchUpInside)

view.addSubview(button)
view.backgroundColor = .white
view.addSubview(demoButton)
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
demoButton.center = view.center
demoButton.sizeToFit()
}

func nav(){
Expand Down
28 changes: 16 additions & 12 deletions Tiptoes/Tiptoes/TiptoesNavController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class TiptoesNavController: UINavigationController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

override init(rootViewController: UIViewController) {
super.init(navigationBarClass: TiptoesNavBar.self, toolbarClass: nil)
viewControllers = [rootViewController]
} // 因为子类提供了这个 Designated Method,因此不会自动继承 init(nibName:bundle:) 方法,但是 init(nibName:bundle:) 又是必须实现的,那么就遵循一下吧。可以说这是一个 bug,是应该在父类的 init(nibName:bundle:) 前加入 required 关键字的。
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
Expand All @@ -31,28 +31,32 @@ class TiptoesNavController: UINavigationController {
// Hide the system navigation bar
navigationBar.isHidden = true
interactivePopGestureRecognizer?.addTarget(self, action: #selector(handleTiptoesDisplay(sender:)))
configureNavigationBar()
setupBar()
}

fileprivate func configureNavigationBar() {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

guard let bar = navigationBar as? TiptoesNavBar else { return }
view.addSubview(bar.tiptoes)
view.addSubview(bar.currentTitleLabel)
view.addSubview(bar.priorTitleLabel)
bar.tiptoes.frame = CGRect(x: 0, y: view.frame.height - tiptoesHeight, width: view.frame.width, height: tiptoesHeight)
bar.currentTitleLabel.sizeToFit()
bar.currentTitleLabel.center = CGPoint(x: view.center.x, y: bar.tiptoes.center.y)
bar.priorTitleLabel.frame = bar.currentTitleLabel.frame
}

fileprivate func setupBar() {
guard let bar = navigationBar as? TiptoesNavBar else { return }
view.addSubview(bar.tiptoes)
view.addSubview(bar.currentTitleLabel)
view.addSubview(bar.priorTitleLabel)
}

@objc private func handleTiptoesDisplay(sender: UIGestureRecognizer) {
guard let bar = navigationBar as? TiptoesNavBar else { return }

// You can customize the transition style here
let portionValue = sender.location(in: view).x
let currentAlpha = portionValue / view.frame.width

// Magic number is to fix the bug that when pan gesture goed half and stop
bar.currentTitleLabel.alpha = (1 - currentAlpha) < 0.5 ? (1 - currentAlpha) * 2 : 1 // 1 -> 0
bar.priorTitleLabel.alpha = currentAlpha > 0.5 ? currentAlpha * 2 - 1 : 0 // 0 -> 1
}
Expand Down Expand Up @@ -85,6 +89,7 @@ extension TiptoesNavController: UINavigationBarDelegate {
if numberOfViewControllers > 0 {
if let popTitle = viewControllers[numberOfViewControllers - 1].title {
bar.priorTitleLabel.text = popTitle
bar.priorTitleLabel.sizeToFit()
}
}
return true
Expand All @@ -107,7 +112,7 @@ class TiptoesNavBar: UINavigationBar {
var currentTitleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 8)
label.textColor = UIColor.white
label.textColor = UIColor.black
label.text = "HOME"
label.textAlignment = .center
return label
Expand All @@ -117,14 +122,13 @@ class TiptoesNavBar: UINavigationBar {
var priorTitleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 8)
label.textColor = UIColor.white
label.textColor = UIColor.black
label.textAlignment = .center
return label
}()

var tiptoes: UIView = {
let toes = UIView()
toes.backgroundColor = UIColor(red: 66.0/255.0, green: 69.0/255.0, blue: 78.0/255.0, alpha:1.0)
return toes
}()

Expand Down
16 changes: 14 additions & 2 deletions Tiptoes/Tiptoes/TopMostViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ import UIKit

class TopMostViewController: UIViewController {

lazy var hintLabel: UILabel = {
let l = UILabel()
l.textColor = .black
l.text = "SWIPE TO RETURN"
l.sizeToFit()
return l
}()

override func viewDidLoad() {
super.viewDidLoad()

title = "NEXT"
view.backgroundColor = UIColor(red: 59.0/255.0, green: 62.0/255.0, blue: 67.0/255.0, alpha: 1.0)
view.addSubview(hintLabel)
view.backgroundColor = .white
}

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
hintLabel.center = view.center
}
}

0 comments on commit 0948878

Please sign in to comment.