Skip to content

Commit

Permalink
Update code to Swift 3 version (#41)
Browse files Browse the repository at this point in the history
* Update for Xcode 8 & Swift 3.0
* Address pull request concerns.
* Fix indentation and update API.
  • Loading branch information
wmcbain authored and kevin-hirsch committed Sep 19, 2016
1 parent 773c3e6 commit 4066ea1
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 197 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -31,4 +31,8 @@ DerivedData
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build
Carthage/Build

# Mac OS X
.DS_Store

4 changes: 2 additions & 2 deletions Demo/AppDelegate.swift
Expand Up @@ -14,10 +14,10 @@ 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 {
DropDown.startListeningToKeyboard()

return true
}

}
}
24 changes: 12 additions & 12 deletions Demo/NiceButton.swift
Expand Up @@ -21,43 +21,43 @@ class NiceButton: UIButton {

view.addConstraint(NSLayoutConstraint(
item: view,
attribute: .Height,
relatedBy: .Equal,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .Height,
attribute: .height,
multiplier: 1,
constant: 1
)
)

addConstraint(NSLayoutConstraint(
item: view,
attribute: .Left,
relatedBy: .Equal,
attribute: .left,
relatedBy: .equal,
toItem: self,
attribute: .Left,
attribute: .left,
multiplier: 1,
constant: 0
)
)

addConstraint(NSLayoutConstraint(
item: view,
attribute: .Right,
relatedBy: .Equal,
attribute: .right,
relatedBy: .equal,
toItem: self,
attribute: .Right,
attribute: .right,
multiplier: 1,
constant: 0
)
)

addConstraint(NSLayoutConstraint(
item: view,
attribute: .Bottom,
relatedBy: .Equal,
attribute: .bottom,
relatedBy: .equal,
toItem: self,
attribute: .Bottom,
attribute: .bottom,
multiplier: 1,
constant: 0
)
Expand Down
46 changes: 23 additions & 23 deletions Demo/ViewController.swift
Expand Up @@ -40,69 +40,69 @@ class ViewController: UIViewController {

//MARK: - Actions

@IBAction func chooseArticle(sender: AnyObject) {
@IBAction func chooseArticle(_ sender: AnyObject) {
chooseArticleDropDown.show()
}

@IBAction func changeAmount(sender: AnyObject) {
@IBAction func changeAmount(_ sender: AnyObject) {
amountDropDown.show()
}

@IBAction func choose(sender: AnyObject) {
@IBAction func choose(_ sender: AnyObject) {
chooseDropDown.show()
}

@IBAction func showCenteredDropDown(sender: AnyObject) {
@IBAction func showCenteredDropDown(_ sender: AnyObject) {
centeredDropDown.show()
}

@IBAction func showBarButtonDropDown(sender: AnyObject) {
@IBAction func showBarButtonDropDown(_ sender: AnyObject) {
rightBarDropDown.show()
}

@IBAction func changeDIsmissMode(sender: UISegmentedControl) {
@IBAction func changeDIsmissMode(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: dropDowns.forEach { $0.dismissMode = .Automatic }
case 1: dropDowns.forEach { $0.dismissMode = .OnTap }
case 0: dropDowns.forEach { $0.dismissMode = .automatic }
case 1: dropDowns.forEach { $0.dismissMode = .onTap }
default: break;
}
}

@IBAction func changeDirection(sender: UISegmentedControl) {
@IBAction func changeDirection(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: dropDowns.forEach { $0.direction = .Any }
case 1: dropDowns.forEach { $0.direction = .Bottom }
case 2: dropDowns.forEach { $0.direction = .Top }
case 0: dropDowns.forEach { $0.direction = .any }
case 1: dropDowns.forEach { $0.direction = .bottom }
case 2: dropDowns.forEach { $0.direction = .top }
default: break;
}
}

@IBAction func changeUI(sender: UISegmentedControl) {
@IBAction func changeUI(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0: setupDefaultDropDown()
case 1: customizeDropDown(self)
default: break;
}
}

@IBAction func showKeyboard(sender: AnyObject) {
@IBAction func showKeyboard(_ sender: AnyObject) {
textField.becomeFirstResponder()
}

@IBAction func hideKeyboard(sender: AnyObject) {
@IBAction func hideKeyboard(_ sender: AnyObject) {
view.endEditing(false)
}

func setupDefaultDropDown() {
DropDown.setupDefaultAppearance()

dropDowns.forEach {
$0.cellNib = UINib(nibName: "DropDownCell", bundle: NSBundle(forClass: DropDownCell.self))
$0.cellNib = UINib(nibName: "DropDownCell", bundle: Bundle(for: DropDownCell.self))
$0.customCellConfiguration = nil
}
}

func customizeDropDown(sender: AnyObject) {
func customizeDropDown(_ sender: AnyObject) {
let appearance = DropDown.appearance()

appearance.cellHeight = 60
Expand All @@ -114,7 +114,7 @@ class ViewController: UIViewController {
appearance.shadowOpacity = 0.9
appearance.shadowRadius = 25
appearance.animationduration = 0.25
appearance.textColor = .darkGrayColor()
appearance.textColor = .darkGray
// appearance.textFont = UIFont(name: "Georgia", size: 14)

dropDowns.forEach {
Expand All @@ -137,8 +137,8 @@ class ViewController: UIViewController {
super.viewDidLoad()

setupDropDowns()
dropDowns.forEach { $0.dismissMode = .OnTap }
dropDowns.forEach { $0.direction = .Any }
dropDowns.forEach { $0.dismissMode = .onTap }
dropDowns.forEach { $0.direction = .any }

view.addSubview(textField)
}
Expand Down Expand Up @@ -175,7 +175,7 @@ class ViewController: UIViewController {

// Action triggered on selection
chooseArticleDropDown.selectionAction = { [unowned self] (index, item) in
self.chooseArticleButton.setTitle(item, forState: .Normal)
self.chooseArticleButton.setTitle(item, for: .normal)
}

// Action triggered on dropdown cancelation (hide)
Expand Down Expand Up @@ -215,7 +215,7 @@ class ViewController: UIViewController {

// Action triggered on selection
amountDropDown.selectionAction = { [unowned self] (index, item) in
self.amountButton.setTitle(item, forState: .Normal)
self.amountButton.setTitle(item, for: .normal)
}
}

Expand All @@ -236,7 +236,7 @@ class ViewController: UIViewController {

// Action triggered on selection
chooseDropDown.selectionAction = { [unowned self] (index, item) in
self.chooseButton.setTitle(item, forState: .Normal)
self.chooseButton.setTitle(item, for: .normal)
}
}

Expand Down
9 changes: 9 additions & 0 deletions DropDown.xcodeproj/project.pbxproj
Expand Up @@ -290,13 +290,16 @@
TargetAttributes = {
0A76440E1B676C2300BF1A2D = {
CreatedOnToolsVersion = 6.4;
LastSwiftMigration = 0800;
};
0A7644231B676C2300BF1A2D = {
CreatedOnToolsVersion = 6.4;
LastSwiftMigration = 0800;
TestTargetID = 0A76440E1B676C2300BF1A2D;
};
0AB5D8701D0EEECD002D3A17 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -502,6 +505,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.Demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -517,6 +521,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.Demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -533,6 +538,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kevin.hirsch.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
};
name = Debug;
Expand All @@ -545,6 +551,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kevin.hirsch.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Demo.app/Demo";
};
name = Release;
Expand All @@ -567,6 +574,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.DropDown;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -589,6 +597,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.assistoLab.DropDown;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down
Expand Up @@ -10,7 +10,7 @@
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "26F545BD-A325-473F-B7B3-A037533DDB11",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"E4EC3EC9984A396270972820B35B740A9BEF1CCB" : "",
"6575E401F0E075B1FD6E179DAD8CCDF3853A7A8E" : "DropDown"
"6575E401F0E075B1FD6E179DAD8CCDF3853A7A8E" : "DropDown\/"
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "DropDown",
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
Expand Down
14 changes: 7 additions & 7 deletions DropDown/helpers/DPDConstants.swift
Expand Up @@ -24,18 +24,18 @@ internal struct DPDConstant {

internal struct UI {

static let TextColor = UIColor.blackColor()
static let TextFont = UIFont.systemFontOfSize(15)
static let TextColor = UIColor.black
static let TextFont = UIFont.systemFont(ofSize: 15)
static let BackgroundColor = UIColor(white: 0.94, alpha: 1)
static let SelectionBackgroundColor = UIColor(white: 0.89, alpha: 1)
static let SeparatorColor = UIColor.clearColor()
static let SeparatorColor = UIColor.clear
static let CornerRadius: CGFloat = 2
static let RowHeight: CGFloat = 44
static let HeightPadding: CGFloat = 20

struct Shadow {

static let Color = UIColor.darkGrayColor()
static let Color = UIColor.darkGray
static let Offset = CGSize.zero
static let Opacity: Float = 0.4
static let Radius: CGFloat = 8
Expand All @@ -47,9 +47,9 @@ internal struct DPDConstant {
internal struct Animation {

static let Duration = 0.15
static let EntranceOptions: UIViewAnimationOptions = [.AllowUserInteraction, .CurveEaseOut]
static let ExitOptions: UIViewAnimationOptions = [.AllowUserInteraction, .CurveEaseIn]
static let DownScaleTransform = CGAffineTransformMakeScale(0.9, 0.9)
static let EntranceOptions: UIViewAnimationOptions = [.allowUserInteraction, .curveEaseOut]
static let ExitOptions: UIViewAnimationOptions = [.allowUserInteraction, .curveEaseIn]
static let DownScaleTransform = CGAffineTransform(scaleX: 0.9, y: 0.9)

}

Expand Down
30 changes: 15 additions & 15 deletions DropDown/helpers/DPDKeyboardListener.swift
Expand Up @@ -12,9 +12,9 @@ internal final class KeyboardListener {

static let sharedInstance = KeyboardListener()

private(set) var isVisible = false
private(set) var keyboardFrame = CGRectZero
private var isListening = false
fileprivate(set) var isVisible = false
fileprivate(set) var keyboardFrame = CGRect.zero
fileprivate var isListening = false

deinit {
stopListeningToKeyboard()
Expand All @@ -33,36 +33,36 @@ extension KeyboardListener {

isListening = true

NSNotificationCenter.defaultCenter().addObserver(
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow(_:)),
name: UIKeyboardWillShowNotification,
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillHide(_:)),
name: UIKeyboardWillHideNotification,
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}

func stopListeningToKeyboard() {
NSNotificationCenter.defaultCenter().removeObserver(self)
NotificationCenter.default.removeObserver(self)
}

@objc
private func keyboardWillShow(notification: NSNotification) {
fileprivate func keyboardWillShow(_ notification: Notification) {
isVisible = true
keyboardFrame = keyboardFrameFromNotification(notification)
keyboardFrame = keyboardFrame(fromNotification: notification)
}

@objc
private func keyboardWillHide(notification: NSNotification) {
fileprivate func keyboardWillHide(_ notification: Notification) {
isVisible = false
keyboardFrame = keyboardFrameFromNotification(notification)
keyboardFrame = keyboardFrame(fromNotification: notification)
}

private func keyboardFrameFromNotification(notification: NSNotification) -> CGRect {
return (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() ?? CGRectZero
fileprivate func keyboardFrame(fromNotification notification: Notification) -> CGRect {
return ((notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
}

}
}

0 comments on commit 4066ea1

Please sign in to comment.