Skip to content

Commit

Permalink
Only update interactive insets when panning
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Dec 31, 2024
1 parent 7fb7a39 commit 4024f43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions modules/keyboard-avoiding-view/ios/KeyboardAvoidingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import ExpoModulesCore

// This view will be used as a native component. Make sure to inherit from `ExpoView`
// to apply the proper styling (e.g. border radius and shadows).
class KeyboardAvoidingView: ExpoView {
class KeyboardAvoidingView: ExpoView, UIGestureRecognizerDelegate {
private let measurer = UIView()
private let container = UIView()
private var scrollView: ScrollViewWrapper?
private var animationInProgress = false
private var measurerHasObserver = false
private var panning = false

required init(appContext: AppContext? = nil) {
super.init(appContext: appContext)
Expand Down Expand Up @@ -120,13 +121,25 @@ class KeyboardAvoidingView: ExpoView {
context _: UnsafeMutableRawPointer?
) {
if keyPath == "center", object as? NSObject == measurer {
if animationInProgress {
if !panning {
return
}
self.scrollView?.setInsetsFromKeyboardHeight(measurer.frame.height)
}
}

func gestureRecognizer(_ g: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool {
return g.view == other.view
}

@objc func panned(p: UIPanGestureRecognizer) {
if p.state == .began {
panning = true
} else if p.state == .ended {
panning = false
}
}

#if RCT_NEW_ARCH_ENABLED
override func mountChildComponentView(_ childComponentView: UIView, index: Int) {
// FIXME: Use a nativeID to find the ScrollView
Expand All @@ -149,6 +162,9 @@ class KeyboardAvoidingView: ExpoView {
// FIXME: Use a nativeID to find the ScrollView
if index == 0 {
scrollView = ScrollViewWrapper(view: subview)
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panned))
panGestureRecognizer.delegate = self
scrollView?.addGestureRecognizer(panGestureRecognizer)
}
container.insertSubview(subview, at: index)
}
Expand Down
1 change: 1 addition & 0 deletions modules/keyboard-avoiding-view/ios/ScrollViewWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
- (instancetype)initWithView:(UIView *)view;
- (void)setInsetsFromKeyboardHeight:(CGFloat)keyboardHeight;
- (UIView *)view;
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer NS_SWIFT_NAME(addGestureRecognizer(_ gestureRecognizer:));

@end
8 changes: 8 additions & 0 deletions modules/keyboard-avoiding-view/ios/ScrollViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ - (UIView *)view {
return (UIView *)_scrollView;
}

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (!_scrollView) {
return;
}

_scrollView.scrollView.addGestureRecognizer(gestureRecognizer);
}

@end

0 comments on commit 4024f43

Please sign in to comment.