66@implementation AvoidSoftInputManager {
77 AvoidSoftInputAnimationHandler *animationHandler;
88 AvoidSoftInputListener *listener;
9+ CGFloat currentNewSoftInputHeight;
10+ BOOL isDebounceActive;
11+ }
12+
13+ + (int64_t )animationDebounceTimeout
14+ {
15+ return (int64_t )(0.048 * NSEC_PER_SEC);
916}
1017
1118- (instancetype )init
@@ -16,6 +23,8 @@ - (instancetype)init
1623 animationHandler.delegate = self;
1724 listener = [AvoidSoftInputListener new ];
1825 listener.delegate = self;
26+ currentNewSoftInputHeight = 0 ;
27+ isDebounceActive = NO ;
1928 }
2029 return self;
2130}
@@ -119,9 +128,40 @@ - (void)onHeightChangedWithOldSoftInputHeight:(CGFloat)oldSoftInputHeight
119128 isOrientationChange : (BOOL )isOrientationChange
120129{
121130 [self .delegate onHeightChanged: newSoftInputHeight];
122- [animationHandler startAnimationFrom: oldSoftInputHeight
123- to: newSoftInputHeight
124- withOrientationChange: isOrientationChange];
131+ [self debounceAnimation: oldSoftInputHeight
132+ newSoftInputHeight: newSoftInputHeight
133+ isOrientationChange: isOrientationChange];
134+ }
135+
136+ - (void )debounceAnimation : (CGFloat)oldSoftInputHeight
137+ newSoftInputHeight : (CGFloat)newSoftInputHeight
138+ isOrientationChange : (BOOL )isOrientationChange
139+ {
140+ // Save latest target value
141+ currentNewSoftInputHeight = newSoftInputHeight;
142+ // If animation call was already scheduled, let's return early
143+ if (isDebounceActive) {
144+ return ;
145+ }
146+ // Activated debounce flag
147+ isDebounceActive = YES ;
148+ __weak AvoidSoftInputManager *weakSelf = self;
149+ // Debounce animation call for 48ms and invoke with the latest target value
150+ dispatch_after (
151+ dispatch_time (DISPATCH_TIME_NOW, [AvoidSoftInputManager animationDebounceTimeout ]),
152+ dispatch_get_main_queue (),
153+ ^{
154+ AvoidSoftInputManager *strongSelf = weakSelf;
155+ if (!strongSelf) {
156+ return ;
157+ }
158+ CGFloat targetHeight = strongSelf->currentNewSoftInputHeight ;
159+ [strongSelf->animationHandler startAnimationFrom: oldSoftInputHeight
160+ to: targetHeight
161+ withOrientationChange: isOrientationChange];
162+ strongSelf->isDebounceActive = NO ;
163+ strongSelf->currentNewSoftInputHeight = 0 ;
164+ });
125165}
126166
127167@end
0 commit comments