Skip to content

Commit

Permalink
再造 “手机QQ” 侧滑菜单(四)——增加 TabBar
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlui committed May 1, 2015
1 parent a6c777a commit b764e74
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

![pic](http://lvwenhan.com/content/uploadfile/201504/8f171428840820.gif)

###番外篇:【再造 “手机QQ” 侧滑菜单(四)——增加 TabBar】较简单不再成文。
实现效果:

![pic](http://staticonsae.sinaapp.com/images/9.gif)


有问题请直接在文章下面留言。

Expand Down
8 changes: 8 additions & 0 deletions SwiftSideslipLikeQQ.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -45,6 +47,8 @@
4659331F1AD7F60700969878 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
465933201AD7F60700969878 /* SwiftSideslipLikeQQTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftSideslipLikeQQTests.swift; sourceTree = "<group>"; };
4659332A1AD7FB0E00969878 /* HomeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
46617ED41AF3E4C5008F038F /* MainTabBarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainTabBarController.swift; sourceTree = "<group>"; };
46617ED51AF3E4C5008F038F /* MainTabBarController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MainTabBarController.xib; sourceTree = "<group>"; };
466C00871AD832D9002ED354 /* Common.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = "<group>"; };
466C008C1AD9071A002ED354 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
466C008E1AD92447002ED354 /* LeftViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LeftViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -101,6 +105,8 @@
465933131AD7F60700969878 /* LaunchScreen.xib */,
465933081AD7F60700969878 /* Supporting Files */,
466C00871AD832D9002ED354 /* Common.swift */,
46617ED41AF3E4C5008F038F /* MainTabBarController.swift */,
46617ED51AF3E4C5008F038F /* MainTabBarController.xib */,
);
path = SwiftSideslipLikeQQ;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand All @@ -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 */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
<key>IDESourceControlProjectIdentifier</key>
<string>638A92D4-E7ED-492B-A10B-4FBA9A121445</string>
<key>IDESourceControlProjectName</key>
<string>SwiftSideslipLikeQQ</string>
<string>project</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>B50A8E23391344F2DB4CCAF07401A5258B534E2C</key>
<string>github.com:johnlui/SwiftSideslipLikeQQ.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>SwiftSideslipLikeQQ.xcodeproj</string>
<string>SwiftSideslipLikeQQ.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>B50A8E23391344F2DB4CCAF07401A5258B534E2C</key>
Expand Down
Binary file not shown.
48 changes: 46 additions & 2 deletions SwiftSideslipLikeQQ/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7531" systemVersion="14D131" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -264,6 +264,50 @@
</objects>
<point key="canvasLocation" x="1076" y="469"/>
</scene>
<!--联系人-->
<scene sceneID="L9Y-cl-PJI">
<objects>
<viewController storyboardIdentifier="Contacts" title="联系人" id="nBo-Ya-JBc" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="5qT-l7-yly"/>
<viewControllerLayoutGuide type="bottom" id="wl0-up-sXN"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="77m-bO-mbG">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<navigationBar contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="CyB-6H-WdI">
<rect key="frame" x="0.0" y="0.0" width="600" height="64"/>
<constraints>
<constraint firstAttribute="height" constant="64" id="n2x-LQ-0Ov"/>
</constraints>
<items>
<navigationItem title="联系人" id="Qdx-AC-1tx"/>
</items>
</navigationBar>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="联系人" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="e6M-nl-GyN">
<rect key="frame" x="16" y="250" width="568" height="100"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="40"/>
<color key="textColor" red="0.99607843139999996" green="0.70588235290000001" blue="0.20392156859999999" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="CyB-6H-WdI" firstAttribute="leading" secondItem="77m-bO-mbG" secondAttribute="leading" id="6KJ-xs-Agp"/>
<constraint firstItem="wl0-up-sXN" firstAttribute="top" secondItem="e6M-nl-GyN" secondAttribute="bottom" constant="250" id="6VL-Ea-pjQ"/>
<constraint firstItem="e6M-nl-GyN" firstAttribute="centerX" secondItem="CyB-6H-WdI" secondAttribute="centerX" id="Hdq-Ch-Wpi"/>
<constraint firstItem="CyB-6H-WdI" firstAttribute="top" secondItem="77m-bO-mbG" secondAttribute="top" id="KpU-cR-La0"/>
<constraint firstAttribute="trailing" secondItem="CyB-6H-WdI" secondAttribute="trailing" id="Lmz-4P-ysb"/>
<constraint firstItem="e6M-nl-GyN" firstAttribute="centerY" secondItem="77m-bO-mbG" secondAttribute="centerY" id="WaJ-lL-qkc"/>
<constraint firstItem="e6M-nl-GyN" firstAttribute="leading" secondItem="77m-bO-mbG" secondAttribute="leadingMargin" id="jKo-gu-gvY"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="rzp-Za-cub" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1076" y="-300"/>
</scene>
</scenes>
<resources>
<image name="avatar" width="56" height="56"/>
Expand Down
2 changes: 2 additions & 0 deletions SwiftSideslipLikeQQ/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions SwiftSideslipLikeQQ/LeftViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
48 changes: 48 additions & 0 deletions SwiftSideslipLikeQQ/MainTabBarController.swift
Original file line number Diff line number Diff line change
@@ -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.
}
*/

}
53 changes: 53 additions & 0 deletions SwiftSideslipLikeQQ/MainTabBarController.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MainTabBarController" customModule="SwiftSideslipLikeQQ" customModuleProvider="target"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tabBarController id="MVf-aK-yqi" customClass="MainTabBarController" customModule="SwiftSideslipLikeQQ" customModuleProvider="target">
<nil key="simulatedBottomBarMetrics"/>
<tabBar key="tabBar" contentMode="scaleToFill" id="3se-QO-4d3">
<rect key="frame" x="0.0" y="0.0" width="320" height="49"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</tabBar>
<viewControllers>
<viewController id="ArV-4A-9pJ">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="2Db-AX-v7B"/>
<viewControllerLayoutGuide type="bottom" id="ols-ay-RfO"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="OZl-hv-SzX">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<tabBarItem key="tabBarItem" title="消息" image="qq" id="lhs-1a-7cs"/>
</viewController>
<viewController id="dAW-AX-gaf">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="vfC-cQ-9ms"/>
<viewControllerLayoutGuide type="bottom" id="RaY-uK-dmt"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Wic-8C-sSl">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<tabBarItem key="tabBarItem" tag="1" systemItem="contacts" id="fje-4a-U9w"/>
</viewController>
<viewController automaticallyAdjustsScrollViewInsets="NO" id="4UF-Yf-2Kt">
<tabBarItem key="tabBarItem" tag="2" title="动态" image="xingxing" id="FSG-M9-Tgi"/>
<toolbarItems/>
</viewController>
</viewControllers>
<point key="canvasLocation" x="236" y="269"/>
</tabBarController>
</objects>
<resources>
<image name="qq" width="30" height="30"/>
<image name="xingxing" width="26" height="27"/>
</resources>
</document>
1 change: 1 addition & 0 deletions SwiftSideslipLikeQQ/OtherPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class OtherPageViewController: UIViewController {

func goBack() {
self.navigationController?.popViewControllerAnimated(true)
Common.rootViewController.mainTabBarController.tabBar.hidden = false
}

/*
Expand Down
26 changes: 20 additions & 6 deletions SwiftSideslipLikeQQ/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import UIKit

class ViewController: UIViewController {

var mainTabBarController: MainTabBarController!
var tapGesture: UITapGestureRecognizer!

var homeNavigationController: UINavigationController!
var homeViewController: HomeViewController!
var leftViewController: LeftViewController!
Expand Down Expand Up @@ -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")
Expand All @@ -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() {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit b764e74

Please sign in to comment.