Skip to content

Commit c410783

Browse files
committed
perf: pause CADisplayLink instead of re-creation
1 parent fb95fc1 commit c410783

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

ios/observers/KeyboardMovementObserver.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class KeyboardMovementObserver: NSObject {
3232

3333
private var _windowsCount: Int = 0
3434
private var prevKeyboardPosition = 0.0
35-
private var displayLink: CADisplayLink?
35+
private var displayLink: CADisplayLink!
3636
private var interactiveKeyboardObserver: NSKeyValueObservation?
3737
private var isMounted = false
3838
// state variables
@@ -57,6 +57,18 @@ public class KeyboardMovementObserver: NSObject {
5757
self.onNotify = onNotify
5858
self.onRequestAnimation = onRequestAnimation
5959
self.onCancelAnimation = onCancelAnimation
60+
61+
super.init()
62+
63+
displayLink = CADisplayLink(target: self, selector: #selector(updateKeyboardFrame))
64+
displayLink.preferredFramesPerSecond = 120 // will fallback to 60 fps for devices without Pro Motion display
65+
displayLink.add(to: .main, forMode: .common)
66+
displayLink.isPaused = true
67+
}
68+
69+
deinit {
70+
displayLink.invalidate()
71+
displayLink = nil
6072
}
6173

6274
@objc public func mount() {
@@ -109,7 +121,7 @@ public class KeyboardMovementObserver: NSObject {
109121

110122
private func keyboardDidMoveInteractively(changeValue: CGPoint) {
111123
// if we are currently animating keyboard -> we need to ignore values from KVO
112-
if displayLink != nil {
124+
if !displayLink.isPaused {
113125
return
114126
}
115127
// if keyboard height is not equal to its bounds - we can ignore
@@ -232,18 +244,15 @@ public class KeyboardMovementObserver: NSObject {
232244
// sometimes `will` events can be called multiple times.
233245
// To avoid double re-creation of listener we are adding this condition
234246
// (if active link is present, then no need to re-setup a listener)
235-
if displayLink != nil {
247+
if !displayLink.isPaused {
236248
return
237249
}
238250

239-
displayLink = CADisplayLink(target: self, selector: #selector(updateKeyboardFrame))
240-
displayLink?.preferredFramesPerSecond = 120 // will fallback to 60 fps for devices without Pro Motion display
241-
displayLink?.add(to: .main, forMode: .common)
251+
displayLink.isPaused = false
242252
}
243253

244254
@objc func removeKeyboardWatcher() {
245-
displayLink?.invalidate()
246-
displayLink = nil
255+
displayLink.isPaused = true
247256
}
248257

249258
func initializeAnimation(fromValue: Double, toValue: Double) {

0 commit comments

Comments
 (0)