Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 0c2174b

Browse files
committed
improved notification handlers
1 parent 454e76a commit 0c2174b

File tree

4 files changed

+118
-76
lines changed

4 files changed

+118
-76
lines changed

KeyboardAvoiding.podspec

Lines changed: 0 additions & 20 deletions
This file was deleted.

KeyboardAvoiding.xcodeproj/project.pbxproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
TargetAttributes = {
107107
16B3B8D82211C1000034C516 = {
108108
CreatedOnToolsVersion = 10.1;
109-
LastSwiftMigration = 1010;
109+
LastSwiftMigration = 1100;
110110
};
111111
};
112112
};
@@ -116,6 +116,7 @@
116116
hasScannedForEncodings = 0;
117117
knownRegions = (
118118
en,
119+
Base,
119120
);
120121
mainGroup = 16B3B8CF2211C1000034C516;
121122
productRefGroup = 16B3B8DA2211C1000034C516 /* Products */;
@@ -201,7 +202,7 @@
201202
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
202203
GCC_WARN_UNUSED_FUNCTION = YES;
203204
GCC_WARN_UNUSED_VARIABLE = YES;
204-
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
205+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
205206
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
206207
MTL_FAST_MATH = YES;
207208
ONLY_ACTIVE_ARCH = YES;
@@ -259,7 +260,7 @@
259260
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
260261
GCC_WARN_UNUSED_FUNCTION = YES;
261262
GCC_WARN_UNUSED_VARIABLE = YES;
262-
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
263+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
263264
MTL_ENABLE_DEBUG_INFO = NO;
264265
MTL_FAST_MATH = YES;
265266
SDKROOT = iphoneos;
@@ -293,7 +294,7 @@
293294
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
294295
SKIP_INSTALL = YES;
295296
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
296-
SWIFT_VERSION = 4.2;
297+
SWIFT_VERSION = 5.0;
297298
TARGETED_DEVICE_FAMILY = "1,2";
298299
};
299300
name = Debug;
@@ -319,7 +320,7 @@
319320
PRODUCT_BUNDLE_IDENTIFIER = com.tiborbodecs.KeyboardAvoiding;
320321
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
321322
SKIP_INSTALL = YES;
322-
SWIFT_VERSION = 4.2;
323+
SWIFT_VERSION = 5.0;
323324
TARGETED_DEVICE_FAMILY = "1,2";
324325
};
325326
name = Release;

README.md

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Watch out ! We've got a keyboard here!
44

5-
6-
75
## Installation
86

97
### Swift Package Manager
@@ -12,19 +10,6 @@ Watch out ! We've got a keyboard here!
1210
.package(url: "https://github.com/CoreKit/KeyboardAvoiding", from: "1.0.0"),
1311
```
1412

15-
### CocoaPods
16-
17-
```
18-
source 'https://github.com/CoreKit/KeyboardAvoiding.git'
19-
pod 'KeyboardAvoiding'
20-
```
21-
22-
### Carthage
23-
24-
```
25-
github "CoreKit/KeyboardAvoiding" "1.0.0"
26-
```
27-
2813
## Usage
2914

3015
```swift
@@ -38,12 +23,21 @@ class ViewController: UIViewController {
3823
override func viewDidLoad() {
3924
super.viewDidLoad()
4025

41-
self.keyboardAvoiding = KeyboardAvoiding({ rect in
42-
//do your stuff on show keyboard
43-
}, {
44-
//do your stuff on hide keyboard
45-
})
26+
self.keyboardAvoiding = KeyboardAvoiding()
27+
.onKeyboardWillShow { rect in
28+
// do your stuff here
29+
}
30+
.onKeyboardDidShow { rect in
31+
// do your stuff here
32+
}
33+
.onKeyboardWillHide {
34+
// do your stuff here
35+
}
36+
.onKeyboardDidHide {
37+
// do your stuff here
38+
}
4639
}
40+
4741
override func viewWillAppear(_ animated: Bool) {
4842
super.viewWillAppear(animated)
4943

Sources/KeyboardAvoiding.swift

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,133 @@
99
import UIKit
1010

1111
open class KeyboardAvoiding {
12-
13-
open var showAnimationBlock: ((CGRect) -> Void)?
14-
open var hideAnimationBlock: (() -> Void)?
15-
16-
public init(_ showAnimationBlock: ((CGRect) -> Void)? = nil,
17-
_ hideAnimationBlock: (() -> Void)? = nil) {
18-
19-
self.showAnimationBlock = showAnimationBlock
20-
self.hideAnimationBlock = hideAnimationBlock
12+
13+
open var willShow: ((CGRect) -> Void)?
14+
open var didShow: ((CGRect) -> Void)?
15+
open var willHide: (() -> Void)?
16+
open var didHide: (() -> Void)?
17+
18+
public init(willShow: ((CGRect) -> Void)? = nil,
19+
didShow: ((CGRect) -> Void)? = nil,
20+
willHide: (() -> Void)? = nil,
21+
didHide: (() -> Void)? = nil) {
22+
23+
self.willShow = willShow
24+
self.didShow = didShow
25+
self.willHide = willHide
26+
self.didHide = didHide
2127
}
22-
23-
open func start() {
28+
29+
public func onKeyboardWillShow(_ willShow: @escaping ((CGRect) -> Void)) -> KeyboardAvoiding {
30+
self.willShow = willShow
31+
return self
32+
}
33+
34+
public func onKeyboardDidShow(_ didShow: @escaping ((CGRect) -> Void)) -> KeyboardAvoiding {
35+
self.didShow = didShow
36+
return self
37+
}
38+
39+
public func onKeyboardWillHide(_ willHide: @escaping (() -> Void)) -> KeyboardAvoiding {
40+
self.willHide = willHide
41+
return self
42+
}
43+
44+
public func onKeyboardDidHide(_ didHide: @escaping (() -> Void)) -> KeyboardAvoiding {
45+
self.didHide = didHide
46+
return self
47+
}
48+
49+
public func start() {
2450
NotificationCenter.default.addObserver(self,
25-
selector: #selector(self.onKeyboardShow(_:)),
51+
selector: #selector(self.keyboardWillShow(_:)),
2652
name: UIResponder.keyboardWillShowNotification,
2753
object: nil)
2854

2955
NotificationCenter.default.addObserver(self,
30-
selector: #selector(self.onKeyboardHide(_:)),
56+
selector: #selector(self.keyboardWillHide(_:)),
3157
name: UIResponder.keyboardWillHideNotification,
3258
object: nil)
59+
60+
NotificationCenter.default.addObserver(self,
61+
selector: #selector(self.keyboardDidShow(_:)),
62+
name: UIResponder.keyboardDidShowNotification,
63+
object: nil)
64+
65+
NotificationCenter.default.addObserver(self,
66+
selector: #selector(self.keyboardDidHide(_:)),
67+
name: UIResponder.keyboardDidHideNotification,
68+
object: nil)
3369
}
3470

35-
open func stop() {
71+
public func stop() {
3672
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
73+
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
3774
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
75+
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidHideNotification, object: nil)
76+
}
77+
78+
private func animate(curve: Int, duration: Double, _ block: @escaping (() -> Void)) {
79+
let animationCurve = UIView.AnimationCurve(rawValue: curve) ?? .linear
80+
let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16))
81+
82+
UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: block, completion: nil)
3883
}
3984

40-
@objc func onKeyboardShow(_ notification: Notification) {
85+
@objc func keyboardWillShow(_ notification: Notification) {
4186
guard
4287
let userInfo = notification.userInfo,
43-
let keyboardFrameFinal = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
4488
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
45-
let curveInt = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int
89+
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int,
90+
let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
4691
else {
4792
return
4893
}
4994

50-
let animationCurve = UIView.AnimationCurve(rawValue: curveInt) ?? .linear
51-
let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16))
52-
53-
UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: {
54-
self.showAnimationBlock?(keyboardFrameFinal)
55-
}, completion: nil)
95+
self.animate(curve: curve, duration: duration) {
96+
self.willShow?(frame)
97+
}
5698
}
5799

58-
@objc func onKeyboardHide(_ notification: Notification) {
100+
@objc func keyboardDidShow(_ notification: Notification) {
59101
guard
60102
let userInfo = notification.userInfo,
61103
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
62-
let curveInt = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int
104+
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int,
105+
let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
63106
else {
64107
return
65108
}
66109

67-
let animationCurve = UIView.AnimationCurve(rawValue: curveInt) ?? .linear
68-
let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16))
69-
70-
UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: {
71-
self.hideAnimationBlock?()
72-
}, completion: nil)
110+
self.animate(curve: curve, duration: duration) {
111+
self.didShow?(frame)
112+
}
113+
}
114+
115+
@objc func keyboardWillHide(_ notification: Notification) {
116+
guard
117+
let userInfo = notification.userInfo,
118+
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
119+
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int
120+
else {
121+
return
122+
}
123+
self.animate(curve: curve, duration: duration) {
124+
self.willHide?()
125+
}
126+
}
127+
128+
@objc func keyboardDidHide(_ notification: Notification) {
129+
guard
130+
let userInfo = notification.userInfo,
131+
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
132+
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int
133+
else {
134+
return
135+
}
136+
137+
self.animate(curve: curve, duration: duration) {
138+
self.didHide?()
139+
}
73140
}
74141
}

0 commit comments

Comments
 (0)