diff --git a/README.md b/README.md index 72da453..8942f5d 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ ![pic](http://lvwenhan.com/content/uploadfile/201504/8f171428840820.gif) +###番外篇:【再造 “手机QQ” 侧滑菜单(四)——增加 TabBar】较简单不再成文。 +实现效果: + +![pic](http://staticonsae.sinaapp.com/images/9.gif) + 有问题请直接在文章下面留言。 diff --git a/SwiftSideslipLikeQQ.xcodeproj/project.pbxproj b/SwiftSideslipLikeQQ.xcodeproj/project.pbxproj index b4135a3..1d10b90 100644 --- a/SwiftSideslipLikeQQ.xcodeproj/project.pbxproj +++ b/SwiftSideslipLikeQQ.xcodeproj/project.pbxproj @@ -16,6 +16,8 @@ 465933151AD7F60700969878 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 465933131AD7F60700969878 /* LaunchScreen.xib */; }; 465933211AD7F60700969878 /* SwiftSideslipLikeQQTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 465933201AD7F60700969878 /* SwiftSideslipLikeQQTests.swift */; }; 4659332B1AD7FB0E00969878 /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4659332A1AD7FB0E00969878 /* HomeViewController.swift */; }; + 46617ED61AF3E4C5008F038F /* MainTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46617ED41AF3E4C5008F038F /* MainTabBarController.swift */; }; + 46617ED71AF3E4C5008F038F /* MainTabBarController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 46617ED51AF3E4C5008F038F /* MainTabBarController.xib */; }; 466C00881AD832D9002ED354 /* Common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466C00871AD832D9002ED354 /* Common.swift */; }; 466C008D1AD9071A002ED354 /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = 466C008C1AD9071A002ED354 /* README.md */; }; 466C008F1AD92447002ED354 /* LeftViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 466C008E1AD92447002ED354 /* LeftViewController.swift */; }; @@ -45,6 +47,8 @@ 4659331F1AD7F60700969878 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 465933201AD7F60700969878 /* SwiftSideslipLikeQQTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftSideslipLikeQQTests.swift; sourceTree = ""; }; 4659332A1AD7FB0E00969878 /* HomeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = ""; }; + 46617ED41AF3E4C5008F038F /* MainTabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainTabBarController.swift; sourceTree = ""; }; + 46617ED51AF3E4C5008F038F /* MainTabBarController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MainTabBarController.xib; sourceTree = ""; }; 466C00871AD832D9002ED354 /* Common.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = ""; }; 466C008C1AD9071A002ED354 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 466C008E1AD92447002ED354 /* LeftViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LeftViewController.swift; sourceTree = ""; }; @@ -101,6 +105,8 @@ 465933131AD7F60700969878 /* LaunchScreen.xib */, 465933081AD7F60700969878 /* Supporting Files */, 466C00871AD832D9002ED354 /* Common.swift */, + 46617ED41AF3E4C5008F038F /* MainTabBarController.swift */, + 46617ED51AF3E4C5008F038F /* MainTabBarController.xib */, ); path = SwiftSideslipLikeQQ; sourceTree = ""; @@ -212,6 +218,7 @@ files = ( 465933101AD7F60700969878 /* Main.storyboard in Resources */, 465933151AD7F60700969878 /* LaunchScreen.xib in Resources */, + 46617ED71AF3E4C5008F038F /* MainTabBarController.xib in Resources */, 465933121AD7F60700969878 /* Images.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -232,6 +239,7 @@ files = ( 4642DDB51ADA76DA00D0F32B /* SwiftColors.swift in Sources */, 466C00881AD832D9002ED354 /* Common.swift in Sources */, + 46617ED61AF3E4C5008F038F /* MainTabBarController.swift in Sources */, 4659330D1AD7F60700969878 /* ViewController.swift in Sources */, 466C008D1AD9071A002ED354 /* README.md in Sources */, 466C008F1AD92447002ED354 /* LeftViewController.swift in Sources */, diff --git a/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcshareddata/SwiftSideslipLikeQQ.xccheckout b/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcshareddata/SwiftSideslipLikeQQ.xccheckout index 972697c..ade6ae0 100644 --- a/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcshareddata/SwiftSideslipLikeQQ.xccheckout +++ b/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcshareddata/SwiftSideslipLikeQQ.xccheckout @@ -7,14 +7,14 @@ IDESourceControlProjectIdentifier 638A92D4-E7ED-492B-A10B-4FBA9A121445 IDESourceControlProjectName - SwiftSideslipLikeQQ + project IDESourceControlProjectOriginsDictionary B50A8E23391344F2DB4CCAF07401A5258B534E2C github.com:johnlui/SwiftSideslipLikeQQ.git IDESourceControlProjectPath - SwiftSideslipLikeQQ.xcodeproj + SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace IDESourceControlProjectRelativeInstallPathDictionary B50A8E23391344F2DB4CCAF07401A5258B534E2C diff --git a/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate index ed414de..42a3e69 100644 Binary files a/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace/xcuserdata/dren_a.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/SwiftSideslipLikeQQ/Base.lproj/Main.storyboard b/SwiftSideslipLikeQQ/Base.lproj/Main.storyboard index 37c4c80..944829e 100644 --- a/SwiftSideslipLikeQQ/Base.lproj/Main.storyboard +++ b/SwiftSideslipLikeQQ/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -264,6 +264,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SwiftSideslipLikeQQ/Common.swift b/SwiftSideslipLikeQQ/Common.swift index e6d7fae..ff2a77b 100644 --- a/SwiftSideslipLikeQQ/Common.swift +++ b/SwiftSideslipLikeQQ/Common.swift @@ -11,4 +11,6 @@ import UIKit struct Common { static let screenWidth = UIScreen.mainScreen().applicationFrame.maxX static let screenHeight = UIScreen.mainScreen().applicationFrame.maxY + static let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController as! ViewController + static let contactsVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Contacts") as! UIViewController } diff --git a/SwiftSideslipLikeQQ/LeftViewController.swift b/SwiftSideslipLikeQQ/LeftViewController.swift index 6f5fc3d..ecd90ba 100644 --- a/SwiftSideslipLikeQQ/LeftViewController.swift +++ b/SwiftSideslipLikeQQ/LeftViewController.swift @@ -34,13 +34,13 @@ class LeftViewController: UIViewController, UITableViewDelegate, UITableViewData } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - let viewController = UIApplication.sharedApplication().keyWindow?.rootViewController as! ViewController - + let viewController = Common.rootViewController viewController.homeViewController.titleOfOtherPages = titlesDictionary[indexPath.row] viewController.homeViewController.performSegueWithIdentifier("showOtherPages", sender: self) - + Common.contactsVC.view.removeFromSuperview() + viewController.mainTabBarController.tabBar.hidden = true + viewController.mainTabBarController.selectedIndex = 0 viewController.showHome() - tableView.deselectRowAtIndexPath(indexPath, animated: false) } diff --git a/SwiftSideslipLikeQQ/MainTabBarController.swift b/SwiftSideslipLikeQQ/MainTabBarController.swift new file mode 100644 index 0000000..fdf45a2 --- /dev/null +++ b/SwiftSideslipLikeQQ/MainTabBarController.swift @@ -0,0 +1,48 @@ +// +// MainTabBarController.swift +// SwiftSideslipLikeQQ +// +// Created by JohnLui on 15/5/2. +// Copyright (c) 2015年 com.lvwenhan. All rights reserved. +// + +import UIKit + +class MainTabBarController: UITabBarController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) { + switch item.tag { + case 0: + Common.contactsVC.view.removeFromSuperview() + case 1: + Common.rootViewController.mainTabBarController.view.addSubview(Common.contactsVC.view) + Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar) + case 2: + println("Just like case 1 ↑") + default: + break + } + } + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/SwiftSideslipLikeQQ/MainTabBarController.xib b/SwiftSideslipLikeQQ/MainTabBarController.xib new file mode 100644 index 0000000..9375d17 --- /dev/null +++ b/SwiftSideslipLikeQQ/MainTabBarController.xib @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SwiftSideslipLikeQQ/OtherPageViewController.swift b/SwiftSideslipLikeQQ/OtherPageViewController.swift index 2a5805f..8243cca 100644 --- a/SwiftSideslipLikeQQ/OtherPageViewController.swift +++ b/SwiftSideslipLikeQQ/OtherPageViewController.swift @@ -35,6 +35,7 @@ class OtherPageViewController: UIViewController { func goBack() { self.navigationController?.popViewControllerAnimated(true) + Common.rootViewController.mainTabBarController.tabBar.hidden = false } /* diff --git a/SwiftSideslipLikeQQ/ViewController.swift b/SwiftSideslipLikeQQ/ViewController.swift index c8e1422..00ba60a 100644 --- a/SwiftSideslipLikeQQ/ViewController.swift +++ b/SwiftSideslipLikeQQ/ViewController.swift @@ -10,6 +10,9 @@ import UIKit class ViewController: UIViewController { + var mainTabBarController: MainTabBarController! + var tapGesture: UITapGestureRecognizer! + var homeNavigationController: UINavigationController! var homeViewController: HomeViewController! var leftViewController: LeftViewController! @@ -53,10 +56,20 @@ class ViewController: UIViewController { // 通过 StoryBoard 取出 HomeViewController 的 view,放在背景视图上面 mainView = UIView(frame: self.view.frame) + + let nibContents = NSBundle.mainBundle().loadNibNamed("MainTabBarController", owner: nil, options: nil) + mainTabBarController = nibContents.first as! MainTabBarController + + let tabBarView = mainTabBarController.view + mainView.addSubview(tabBarView) + homeNavigationController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeNavigationController") as! UINavigationController homeViewController = homeNavigationController.viewControllers.first as! HomeViewController - mainView.addSubview(homeViewController.navigationController!.view) - mainView.addSubview(homeViewController.view) + tabBarView.addSubview(homeViewController.navigationController!.view) + tabBarView.addSubview(homeViewController.view) + + tabBarView.bringSubviewToFront(mainTabBarController.tabBar) + self.view.addSubview(mainView) homeViewController.navigationItem.leftBarButtonItem?.action = Selector("showLeft") @@ -67,10 +80,8 @@ class ViewController: UIViewController { panGesture.addTarget(self, action: Selector("pan:")) mainView.addGestureRecognizer(panGesture) - // 绑定单击收起菜单 - let tapGesture = UITapGestureRecognizer(target: self, action: "showHome") - mainView.addGestureRecognizer(tapGesture) - + // 生成单击收起菜单手势 + tapGesture = UITapGestureRecognizer(target: self, action: "showHome") } override func didReceiveMemoryWarning() { @@ -123,17 +134,20 @@ class ViewController: UIViewController { // 展示左视图 func showLeft() { + mainView.addGestureRecognizer(tapGesture) distance = self.view.center.x * (FullDistance*2 + Proportion - 1) doTheAnimate(self.Proportion, showWhat: "left") homeNavigationController.popToRootViewControllerAnimated(true) } // 展示主视图 func showHome() { + mainView.removeGestureRecognizer(tapGesture) distance = 0 doTheAnimate(1, showWhat: "home") } // 展示右视图 func showRight() { + mainView.addGestureRecognizer(tapGesture) distance = self.view.center.x * -(FullDistance*2 + Proportion - 1) doTheAnimate(self.Proportion, showWhat: "right") }