Skip to content

Commit

Permalink
迁移到Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlui committed Oct 17, 2016
1 parent 183e40e commit e780877
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 82 deletions.
22 changes: 11 additions & 11 deletions SwiftColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,31 @@ extension SWColor {

// Check for hash and remove the hash
if hex.hasPrefix("#") {
hex = hex.substringFromIndex(hex.startIndex.advancedBy(1))
hex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 1))
}

if let _ = hex.rangeOfString("(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)", options: .RegularExpressionSearch) {
if let _ = hex.range(of: "(^[0-9A-Fa-f]{6}$)|(^[0-9A-Fa-f]{3}$)", options: .regularExpression) {

// Deal with 3 character Hex strings
if hex.characters.count == 3 {
let redHex = hex.substringToIndex(hex.startIndex.advancedBy(1))
let greenHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(1), end: hex.startIndex.advancedBy(2)))
let blueHex = hex.substringFromIndex(hex.startIndex.advancedBy(2))
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 1))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 1) ..< hex.characters.index(hex.startIndex, offsetBy: 2)))
let blueHex = hex.substring(from: hex.characters.index(hex.startIndex, offsetBy: 2))

hex = redHex + redHex + greenHex + greenHex + blueHex + blueHex
}

let redHex = hex.substringToIndex(hex.startIndex.advancedBy(2))
let greenHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(2), end: hex.startIndex.advancedBy(4)))
let blueHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(4), end: hex.startIndex.advancedBy(6)))
let redHex = hex.substring(to: hex.characters.index(hex.startIndex, offsetBy: 2))
let greenHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 2) ..< hex.characters.index(hex.startIndex, offsetBy: 4)))
let blueHex = hex.substring(with: (hex.characters.index(hex.startIndex, offsetBy: 4) ..< hex.characters.index(hex.startIndex, offsetBy: 6)))

var redInt: CUnsignedInt = 0
var greenInt: CUnsignedInt = 0
var blueInt: CUnsignedInt = 0

NSScanner(string: redHex).scanHexInt(&redInt)
NSScanner(string: greenHex).scanHexInt(&greenInt)
NSScanner(string: blueHex).scanHexInt(&blueInt)
Scanner(string: redHex).scanHexInt32(&redInt)
Scanner(string: greenHex).scanHexInt32(&greenInt)
Scanner(string: blueHex).scanHexInt32(&blueInt)

self.init(red: CGFloat(redInt) / 255.0, green: CGFloat(greenInt) / 255.0, blue: CGFloat(blueInt) / 255.0, alpha: CGFloat(alpha))
}
Expand Down
13 changes: 12 additions & 1 deletion SwiftSideslipLikeQQ.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,16 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = com.lvwenhan;
TargetAttributes = {
465933041AD7F60700969878 = {
CreatedOnToolsVersion = 6.3;
LastSwiftMigration = 0800;
};
465933191AD7F60700969878 = {
CreatedOnToolsVersion = 6.3;
LastSwiftMigration = 0800;
TestTargetID = 465933041AD7F60700969878;
};
};
Expand Down Expand Up @@ -298,8 +300,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -343,8 +347,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -363,6 +369,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand All @@ -376,6 +383,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -388,6 +396,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -407,6 +416,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftSideslipLikeQQ.app/SwiftSideslipLikeQQ";
};
name = Debug;
Expand All @@ -423,6 +433,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.lvwenhan.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftSideslipLikeQQ.app/SwiftSideslipLikeQQ";
};
name = Release;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0700"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
20 changes: 10 additions & 10 deletions SwiftSideslipLikeQQ/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

// 改变 StatusBar 颜色
application.statusBarStyle = UIStatusBarStyle.LightContent
application.statusBarStyle = UIStatusBarStyle.lightContent

// 改变 navigation bar 的背景色及前景色
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.translucent = false
navigationBarAppearace.isTranslucent = false
navigationBarAppearace.barTintColor = UIColor(hex: 0x25b6ed)
navigationBarAppearace.tintColor = UIColor.whiteColor()
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
navigationBarAppearace.tintColor = UIColor.white
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]

return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
8 changes: 4 additions & 4 deletions SwiftSideslipLikeQQ/Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import UIKit

struct Common {
// Swift 中, static let 才是真正可靠好用的单例模式
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")
static let screenWidth = UIScreen.main.applicationFrame.maxX
static let screenHeight = UIScreen.main.applicationFrame.maxY
static let rootViewController = UIApplication.shared.keyWindow?.rootViewController as! ViewController
static let contactsVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Contacts")
}
8 changes: 4 additions & 4 deletions SwiftSideslipLikeQQ/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class HomeViewController: UIViewController {
// 设置中间 segmentView 视图
let segmentView = UISegmentedControl(items: ["消息", "电话"])
segmentView.selectedSegmentIndex = 0
segmentView.setWidth(60, forSegmentAtIndex: 0)
segmentView.setWidth(60, forSegmentAtIndex: 1)
segmentView.setWidth(60, forSegmentAt: 0)
segmentView.setWidth(60, forSegmentAt: 1)
self.navigationItem.titleView = segmentView
}

Expand All @@ -35,9 +35,9 @@ class HomeViewController: UIViewController {
// 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?) {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showOtherPages" {
if let a = segue.destinationViewController as? OtherPageViewController {
if let a = segue.destination as? OtherPageViewController {
a.PageTitle = titleOfOtherPages
}
}
Expand Down
24 changes: 12 additions & 12 deletions SwiftSideslipLikeQQ/LeftViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LeftViewController: UIViewController, UITableViewDelegate, UITableViewData
settingTableView.tableFooterView = UIView()

heightLayoutConstraintOfSettingTableView.constant = Common.screenHeight < 500 ? Common.screenHeight * (568 - 221) / 568 : 347
self.view.frame = CGRectMake(0, 0, 320 * 0.78, Common.screenHeight)
self.view.frame = CGRect(x: 0, y: 0, width: 320 * 0.78, height: Common.screenHeight)
}

override func didReceiveMemoryWarning() {
Expand All @@ -35,29 +35,29 @@ class LeftViewController: UIViewController, UITableViewDelegate, UITableViewData
}

// 处理点击事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController = Common.rootViewController
viewController.homeViewController.titleOfOtherPages = titlesDictionary[indexPath.row]
viewController.homeViewController.performSegueWithIdentifier("showOtherPages", sender: self)
viewController.homeViewController.titleOfOtherPages = titlesDictionary[(indexPath as NSIndexPath).row]
viewController.homeViewController.performSegue(withIdentifier: "showOtherPages", sender: self)
Common.contactsVC.view.removeFromSuperview()
viewController.mainTabBarController.tabBar.hidden = true
viewController.mainTabBarController.tabBar.isHidden = true
viewController.mainTabBarController.selectedIndex = 0
viewController.showHome()
tableView.deselectRowAtIndexPath(indexPath, animated: false)
tableView.deselectRow(at: indexPath, animated: false)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("leftViewCell", forIndexPath: indexPath)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "leftViewCell", for: indexPath)

cell.backgroundColor = UIColor.clearColor()
cell.textLabel!.text = titlesDictionary[indexPath.row]
cell.backgroundColor = UIColor.clear
cell.textLabel!.text = titlesDictionary[(indexPath as NSIndexPath).row]

return cell
}
Expand Down
4 changes: 2 additions & 2 deletions SwiftSideslipLikeQQ/MainTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class MainTabBarController: UITabBarController {
}

// 覆写了 TabBar 的点击效果
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
case 0:
Common.contactsVC.view.removeFromSuperview()
case 1:
// 这里为了省事采用了简单的 addSubView 方案,真正项目中应该采用 TabBar Controller 自带的 self.viewControllers 方案
Common.rootViewController.mainTabBarController.view.addSubview(Common.contactsVC.view)
Common.rootViewController.mainTabBarController.view.bringSubviewToFront(Common.rootViewController.mainTabBarController.tabBar)
Common.rootViewController.mainTabBarController.view.bringSubview(toFront: Common.rootViewController.mainTabBarController.tabBar)
case 2:
print("Just like case 1 ↑")
default:
Expand Down
8 changes: 4 additions & 4 deletions SwiftSideslipLikeQQ/OtherPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class OtherPageViewController: UIViewController {
mainLabel.text = PageTitle

// 自定义返回按钮
let backButton = UIBarButtonItem(title: "く返回", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
let backButton = UIBarButtonItem(title: "く返回", style: UIBarButtonItemStyle.plain, target: self, action: #selector(OtherPageViewController.goBack))
self.navigationItem.leftBarButtonItem = backButton

// 弥补因为返回按钮被替换导致的边缘滑入手势失效的问题
let gesture = UIPanGestureRecognizer(target: self, action: "goBack")
let gesture = UIPanGestureRecognizer(target: self, action: #selector(OtherPageViewController.goBack))
self.view.addGestureRecognizer(gesture)
}

Expand All @@ -35,8 +35,8 @@ class OtherPageViewController: UIViewController {
}

func goBack() {
self.navigationController?.popViewControllerAnimated(true)
Common.rootViewController.mainTabBarController.tabBar.hidden = false
_ = self.navigationController?.popViewController(animated: true)
Common.rootViewController.mainTabBarController.tabBar.isHidden = false
}

/*
Expand Down
Loading

0 comments on commit e780877

Please sign in to comment.