From c1e3473637dbfe77f657cb1023c8dd6379d39b12 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Thu, 14 Apr 2016 12:53:22 +0200 Subject: [PATCH 1/8] Added support for animating in JS before changing the webview size when keyboard appears/disappears --- src/ios/CDVKeyboard.h | 2 + src/ios/CDVKeyboard.m | 106 +++++++++++++++++++++++++++++++++++++++++- www/keyboard.js | 33 +++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) diff --git a/src/ios/CDVKeyboard.h b/src/ios/CDVKeyboard.h index 0b68cc5..7745940 100644 --- a/src/ios/CDVKeyboard.h +++ b/src/ios/CDVKeyboard.h @@ -39,5 +39,7 @@ - (void)disableScrollingInShrinkView:(CDVInvokedUrlCommand*)command; - (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command; - (void)hide:(CDVInvokedUrlCommand*)command; +- (void)animationStart:(CDVInvokedUrlCommand *)command; +- (void)animationComplete:(CDVInvokedUrlCommand *)command; @end diff --git a/src/ios/CDVKeyboard.m b/src/ios/CDVKeyboard.m index acd039a..ddf770e 100644 --- a/src/ios/CDVKeyboard.m +++ b/src/ios/CDVKeyboard.m @@ -31,7 +31,20 @@ @interface CDVKeyboard () @end -@implementation CDVKeyboard +@interface AnimationDetails : NSObject +@property (nonatomic, readwrite, assign) CGFloat from; +@property (nonatomic, readwrite, assign) CGFloat to; +@property (nonatomic, readwrite, assign) CGRect screen; +@end + +@implementation AnimationDetails + +@end; + +@implementation CDVKeyboard { + AnimationDetails *_animationDetails; + BOOL _shouldAnimateWebView; +} - (id)settingForKey:(NSString*)key { @@ -66,12 +79,14 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { + NSLog(@"KeyboardShown"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShow();"]; }]; _keyboardHideObserver = [nc addObserverForName:UIKeyboardDidHideNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { + NSLog(@"KeyboardHidden"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHide();"]; }]; @@ -79,6 +94,7 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { + NSLog(@"KeyboardWillShow"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShowing();"]; weakSelf.keyboardIsVisible = YES; }]; @@ -86,6 +102,7 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { + NSLog(@"KeyboardWillHide"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHiding();"]; weakSelf.keyboardIsVisible = NO; }]; @@ -179,6 +196,8 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif screen = full; } + CGFloat currentScreenHeight = self.webView.frame.size.height; + // Get the intersection of the keyboard and screen and move the webview above it // Note: we check for _shrinkView at this point instead of the beginning of the method to handle // the case where the user disabled shrinkView while the keyboard is showing. @@ -189,8 +208,61 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif self.webView.scrollView.scrollEnabled = !self.disableScrollingInShrinkView; } + CGFloat newScreenHeight = screen.size.height; + // When keyboard will be hidden, willShow and show is triggered again + // even though keyboard is already visible, ignoring as early as possible + if(newScreenHeight == self.webView.frame.size.height) + { + NSLog(@"View supposed to change to same height, ignoring"); + if(_shouldAnimateWebView) + { + _shouldAnimateWebView = NO; + } + return; + } + // A view's frame is in its superview's coordinate system so we need to convert again - self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; + if(!_shouldAnimateWebView) + { + self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; + return; + } + + NSDictionary* userInfo = [notif userInfo]; + NSNumber *durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey]; + NSTimeInterval duration = durationValue.doubleValue; + + // Tell JS that it can start animating with values + NSString *javascriptString = [NSString stringWithFormat:@"Keyboard.beginAnimation(%f, %f, %f)", currentScreenHeight, newScreenHeight, duration*1000]; + NSLog(@"Is changing webview, newScreenHeight: %f", newScreenHeight); + + BOOL isGrowing = newScreenHeight > currentScreenHeight; + + // If webView is growing, change it's frame imediately, so it's content is not clipped during animation + if (isGrowing) { + self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; + NSLog(@"Frame changed before animation!"); + } + [self.commandDelegate evalJs: javascriptString]; + + _animationDetails = [[AnimationDetails alloc] init]; + _animationDetails.from = currentScreenHeight; + _animationDetails.to = newScreenHeight; + _animationDetails.screen = screen; + + // alternative to using animationComplete but the timer can finish before + // the browser is finished animating, thereby clipping the animation + +// __weak typeof(self) weakSelf = self; +// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ +// __strong typeof(weakSelf) self = weakSelf; +// // If webview was shrinking, change it's frame after animation is complete +// if (!isGrowing) { +// self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; +// NSLog(@"Frame changed after animation!"); +// } +// NSLog(@"Animation duration should be completed!"); +// }); } #pragma mark UIScrollViewDelegate @@ -243,6 +315,36 @@ - (void)hide:(CDVInvokedUrlCommand*)command [self.webView endEditing:YES]; } +// JS indicates that it wants to handle Keyboard animation +- (void)animationStart:(CDVInvokedUrlCommand *)command +{ + NSLog(@"animationStart"); + _shouldAnimateWebView = YES; +} + +// JS indicates that it finished handling Keyboard animation +- (void)animationComplete:(CDVInvokedUrlCommand*)command +{ + NSLog(@"animation Complete received"); + if(!_animationDetails) + { + NSLog(@"no animation details available"); + return; + } + + NSLog(@"animation Complete received from %f to %f", [_animationDetails from] , [_animationDetails to]); + + BOOL isGrowing = [_animationDetails from] < [_animationDetails to]; + // If webview was shrinking, change it's frame after animation is complete + if (!isGrowing) { + self.webView.frame = [self.webView.superview convertRect:[_animationDetails screen] fromView:self.webView]; + NSLog(@"Frame changed after animation!"); + } + NSLog(@"Animation duration should be completed!"); + _shouldAnimateWebView = NO; + _animationDetails = nil; +} + #pragma mark dealloc - (void)dealloc diff --git a/www/keyboard.js b/www/keyboard.js index 2f3c9f2..883be43 100644 --- a/www/keyboard.js +++ b/www/keyboard.js @@ -70,6 +70,10 @@ Keyboard.fireOnHiding = function() { if(Keyboard.onhiding) { Keyboard.onhiding(); } + if(typeof Keyboard.onKeyboardAnimate === 'function') + { + animationStart(); + } }; Keyboard.fireOnShowing = function() { @@ -78,6 +82,10 @@ Keyboard.fireOnShowing = function() { if(Keyboard.onshowing) { Keyboard.onshowing(); } + if(typeof Keyboard.onKeyboardAnimate === 'function') + { + animationStart(); + } }; Keyboard.show = function() { @@ -88,6 +96,31 @@ Keyboard.hide = function() { exec(null, null, "Keyboard", "hide", []); }; +// Private animation handling +var animationStart = function() { + console.log('JS: animationStart'); + exec(null, null, "Keyboard", "animationStart", []); +}; + +var animationComplete = function() { + console.log('JS: animationComplete'); + exec(null, null, "Keyboard", "animationComplete", []); +}; + +Keyboard.beginAnimation = function(from, to, duration) { + console.log('JS: beginAnimation'); + if(typeof Keyboard.onKeyboardAnimate === 'function') + { + Keyboard.onKeyboardAnimate(from, to, duration, animationComplete); + } + else + { + animationComplete(); + } +}; +// End private animation handling + +Keyboard.onKeyboardAnimate = null; Keyboard.isVisible = false; Keyboard.automaticScrollToTopOnHiding = false; From dc39380686330880b5a690911c039d13a01702d0 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Thu, 14 Apr 2016 13:02:18 +0200 Subject: [PATCH 2/8] Updated README. --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index d7ba0af..88c6b10 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ This plugin was based on this Apache [project](https://github.com/apache/cordova - [Properties](#properties) - [Keyboard.isVisible](#keyboardisvisible) - [Keyboard.automaticScrollToTopOnHiding](#keyboardautomaticscrolltotoponhiding) + - [Keyboard.onKeyboardAnimate](#keyboardanimate) - [Events](#events) - [keyboardDidShow](#keyboarddidshow) - [keyboardDidHide](#keyboarddidhide) @@ -170,6 +171,36 @@ This is allows to fix issue with elements declared with position: fixed, after keyboard is hiding. +#### Supported Platforms + +- iOS + +## Keyboard.onKeyboardAnimate + +Assign a function that will handle content animation in the browser + + Keyboard.onKeyboardAnimate = function(fromHeight, toHeight, animationDurationInMS, animationCompleteCallback) + { + // Example with jQuery (http://jquery.com/) and Velocity.JS (http://julian.com/research/velocity/) + $('body').velocity({height: [to, from]}, {duration: animationDurationInMS, easing: "easeOutQuad", complete: function() + { + // Tells the plugin that client side has finished animation + animationCompleteCallback(); + }}); + }; + +#### Description + +Assign a function that will handle animation when the keyboard appears and disappears. +Remember to execute the provided callback to let the plugin know when animation has finished. +The parameters given is which height the webview will animate from and to, the duration for which the keyboard will animate and a callback method to call when the animation has been completed. + +Worth noting is that window.resize will be triggered after animation has been completed when keyboard is showing, but before animation is started when hiding. +This is due to the fact that the webview will resize after animation has been completed when the keyboard is appearing so that the animation is not clipped, and +when the keyboard is hiding it will resize to the full height so that the animation out can be completed without clipping as well. + +Thanks to @glyuck for this approach from his project [GLKAnimateWebViewFrame](https://github.com/glyuck/GLKAnimateWebViewFrame) which is the base for this feature + #### Supported Platforms - iOS From 66c7ff7472500cc98fb119b754455a7113b3fd61 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Thu, 14 Apr 2016 13:18:17 +0200 Subject: [PATCH 3/8] Removed logging and tried to adhere to codestyle. --- src/ios/CDVKeyboard.m | 16 ---------------- www/keyboard.js | 22 ++++++---------------- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/ios/CDVKeyboard.m b/src/ios/CDVKeyboard.m index ddf770e..b3a434c 100644 --- a/src/ios/CDVKeyboard.m +++ b/src/ios/CDVKeyboard.m @@ -79,14 +79,12 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { - NSLog(@"KeyboardShown"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShow();"]; }]; _keyboardHideObserver = [nc addObserverForName:UIKeyboardDidHideNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { - NSLog(@"KeyboardHidden"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHide();"]; }]; @@ -94,7 +92,6 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { - NSLog(@"KeyboardWillShow"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShowing();"]; weakSelf.keyboardIsVisible = YES; }]; @@ -102,7 +99,6 @@ - (void)pluginInitialize object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) { - NSLog(@"KeyboardWillHide"); [weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHiding();"]; weakSelf.keyboardIsVisible = NO; }]; @@ -213,7 +209,6 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif // even though keyboard is already visible, ignoring as early as possible if(newScreenHeight == self.webView.frame.size.height) { - NSLog(@"View supposed to change to same height, ignoring"); if(_shouldAnimateWebView) { _shouldAnimateWebView = NO; @@ -234,14 +229,12 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif // Tell JS that it can start animating with values NSString *javascriptString = [NSString stringWithFormat:@"Keyboard.beginAnimation(%f, %f, %f)", currentScreenHeight, newScreenHeight, duration*1000]; - NSLog(@"Is changing webview, newScreenHeight: %f", newScreenHeight); BOOL isGrowing = newScreenHeight > currentScreenHeight; // If webView is growing, change it's frame imediately, so it's content is not clipped during animation if (isGrowing) { self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; - NSLog(@"Frame changed before animation!"); } [self.commandDelegate evalJs: javascriptString]; @@ -259,9 +252,7 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif // // If webview was shrinking, change it's frame after animation is complete // if (!isGrowing) { // self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; -// NSLog(@"Frame changed after animation!"); // } -// NSLog(@"Animation duration should be completed!"); // }); } @@ -318,29 +309,22 @@ - (void)hide:(CDVInvokedUrlCommand*)command // JS indicates that it wants to handle Keyboard animation - (void)animationStart:(CDVInvokedUrlCommand *)command { - NSLog(@"animationStart"); _shouldAnimateWebView = YES; } // JS indicates that it finished handling Keyboard animation - (void)animationComplete:(CDVInvokedUrlCommand*)command { - NSLog(@"animation Complete received"); if(!_animationDetails) { - NSLog(@"no animation details available"); return; } - NSLog(@"animation Complete received from %f to %f", [_animationDetails from] , [_animationDetails to]); - BOOL isGrowing = [_animationDetails from] < [_animationDetails to]; // If webview was shrinking, change it's frame after animation is complete if (!isGrowing) { self.webView.frame = [self.webView.superview convertRect:[_animationDetails screen] fromView:self.webView]; - NSLog(@"Frame changed after animation!"); } - NSLog(@"Animation duration should be completed!"); _shouldAnimateWebView = NO; _animationDetails = nil; } diff --git a/www/keyboard.js b/www/keyboard.js index 883be43..74aa25c 100644 --- a/www/keyboard.js +++ b/www/keyboard.js @@ -70,9 +70,8 @@ Keyboard.fireOnHiding = function() { if(Keyboard.onhiding) { Keyboard.onhiding(); } - if(typeof Keyboard.onKeyboardAnimate === 'function') - { - animationStart(); + if(Keyboard.onKeyboardAnimate) { + animationStart(); } }; @@ -82,9 +81,8 @@ Keyboard.fireOnShowing = function() { if(Keyboard.onshowing) { Keyboard.onshowing(); } - if(typeof Keyboard.onKeyboardAnimate === 'function') - { - animationStart(); + if(Keyboard.onKeyboardAnimate) { + animationStart(); } }; @@ -96,29 +94,21 @@ Keyboard.hide = function() { exec(null, null, "Keyboard", "hide", []); }; -// Private animation handling var animationStart = function() { - console.log('JS: animationStart'); exec(null, null, "Keyboard", "animationStart", []); }; var animationComplete = function() { - console.log('JS: animationComplete'); exec(null, null, "Keyboard", "animationComplete", []); }; Keyboard.beginAnimation = function(from, to, duration) { - console.log('JS: beginAnimation'); - if(typeof Keyboard.onKeyboardAnimate === 'function') - { + if(typeof Keyboard.onKeyboardAnimate === 'function') { Keyboard.onKeyboardAnimate(from, to, duration, animationComplete); - } - else - { + } else { animationComplete(); } }; -// End private animation handling Keyboard.onKeyboardAnimate = null; Keyboard.isVisible = false; From 49bbad89f4791877aee50c0f23f46edebe77c852 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Thu, 14 Apr 2016 13:20:49 +0200 Subject: [PATCH 4/8] Fixed link. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 88c6b10..7e49471 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This plugin was based on this Apache [project](https://github.com/apache/cordova - [Properties](#properties) - [Keyboard.isVisible](#keyboardisvisible) - [Keyboard.automaticScrollToTopOnHiding](#keyboardautomaticscrolltotoponhiding) - - [Keyboard.onKeyboardAnimate](#keyboardanimate) + - [Keyboard.onKeyboardAnimate](#keyboardonkeyboardanimate) - [Events](#events) - [keyboardDidShow](#keyboarddidshow) - [keyboardDidHide](#keyboarddidhide) From eb1c86796d3d7a24f674951d7c422642fe4d1775 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Thu, 14 Apr 2016 13:23:16 +0200 Subject: [PATCH 5/8] Updated README. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7e49471..fa58fea 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ after keyboard is hiding. Assign a function that will handle content animation in the browser - Keyboard.onKeyboardAnimate = function(fromHeight, toHeight, animationDurationInMS, animationCompleteCallback) + Keyboard.onKeyboardAnimate = function(fromHeight, toHeight, animationDurationInMs, animationCompleteCallback) { // Example with jQuery (http://jquery.com/) and Velocity.JS (http://julian.com/research/velocity/) $('body').velocity({height: [to, from]}, {duration: animationDurationInMS, easing: "easeOutQuad", complete: function() @@ -199,7 +199,7 @@ Worth noting is that window.resize will be triggered after animation has been co This is due to the fact that the webview will resize after animation has been completed when the keyboard is appearing so that the animation is not clipped, and when the keyboard is hiding it will resize to the full height so that the animation out can be completed without clipping as well. -Thanks to @glyuck for this approach from his project [GLKAnimateWebViewFrame](https://github.com/glyuck/GLKAnimateWebViewFrame) which is the base for this feature +Thanks to [@glyuck](https://github.com/glyuck) for this approach from his project [GLKAnimateWebViewFrame](https://github.com/glyuck/GLKAnimateWebViewFrame) which is the base for this feature #### Supported Platforms From ca2dff4c170939bddea30cae63920ab7fde53e3f Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Tue, 16 Jan 2018 13:42:32 +0100 Subject: [PATCH 6/8] Refactored to set enable/disable animation instead of relying on calls through JS<->Native bridge have correct timing --- src/ios/CDVKeyboard.m | 40 +++++++++++++++++++++------------------- www/keyboard.js | 31 ++++++++++++++----------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/ios/CDVKeyboard.m b/src/ios/CDVKeyboard.m index a6293fe..377e580 100644 --- a/src/ios/CDVKeyboard.m +++ b/src/ios/CDVKeyboard.m @@ -57,6 +57,7 @@ - (void)pluginInitialize { NSString* setting = nil; + _shouldAnimateWebView = NO; setting = @"HideKeyboardFormAccessoryBar"; if ([self settingForKey:setting]) { self.hideFormAccessoryBar = [(NSNumber*)[self settingForKey:setting] boolValue]; @@ -172,6 +173,7 @@ - (void)setShrinkView:(BOOL)shrinkView - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif { + BOOL shouldAnimate = _shouldAnimateWebView; // No-op on iOS 7.0. It already resizes webview by default, and this plugin is causing layout issues // with fixed position elements. We possibly should attempt to implement shrinkview = false on iOS7.0. // iOS 7.1+ behave the same way as iOS 6 @@ -219,15 +221,11 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif // even though keyboard is already visible, ignoring as early as possible if(newScreenHeight == self.webView.frame.size.height) { - if(_shouldAnimateWebView) - { - _shouldAnimateWebView = NO; - } return; } // A view's frame is in its superview's coordinate system so we need to convert again - if(!_shouldAnimateWebView) + if(!shouldAnimate) { self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; return; @@ -237,15 +235,14 @@ - (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif NSNumber *durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval duration = durationValue.doubleValue; - // Tell JS that it can start animating with values - NSString *javascriptString = [NSString stringWithFormat:@"Keyboard.beginAnimation(%f, %f, %f)", currentScreenHeight, newScreenHeight, duration*1000]; - BOOL isGrowing = newScreenHeight > currentScreenHeight; // If webView is growing, change it's frame imediately, so it's content is not clipped during animation if (isGrowing) { self.webView.frame = [self.webView.superview convertRect:screen fromView:self.webView]; } + // Tell JS that it can start animating with values + NSString *javascriptString = [NSString stringWithFormat:@"Keyboard.beginAnimation(%f, %f, %f)", currentScreenHeight, newScreenHeight, duration*1000]; [self.commandDelegate evalJs: javascriptString]; _animationDetails = [[AnimationDetails alloc] init]; @@ -291,7 +288,7 @@ - (void)shrinkView:(CDVInvokedUrlCommand*)command self.shrinkView = [value boolValue]; } - + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:self.shrinkView] callbackId:command.callbackId]; } @@ -306,7 +303,7 @@ - (void)disableScrollingInShrinkView:(CDVInvokedUrlCommand*)command self.disableScrollingInShrinkView = [value boolValue]; } - + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:self.disableScrollingInShrinkView] callbackId:command.callbackId]; } @@ -318,10 +315,10 @@ - (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command if (!([value isKindOfClass:[NSNumber class]])) { value = [NSNumber numberWithBool:NO]; } - + self.hideFormAccessoryBar = [value boolValue]; } - + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:self.hideFormAccessoryBar] callbackId:command.callbackId]; } @@ -331,12 +328,6 @@ - (void)hide:(CDVInvokedUrlCommand*)command [self.webView endEditing:YES]; } -// JS indicates that it wants to handle Keyboard animation -- (void)animationStart:(CDVInvokedUrlCommand *)command -{ - _shouldAnimateWebView = YES; -} - // JS indicates that it finished handling Keyboard animation - (void)animationComplete:(CDVInvokedUrlCommand*)command { @@ -350,10 +341,21 @@ - (void)animationComplete:(CDVInvokedUrlCommand*)command if (!isGrowing) { self.webView.frame = [self.webView.superview convertRect:[_animationDetails screen] fromView:self.webView]; } - _shouldAnimateWebView = NO; _animationDetails = nil; } +// JS indicates that it wants to handle Keyboard animation +- (void)enableAnimation:(CDVInvokedUrlCommand *)command +{ + _shouldAnimateWebView = YES; +} + +// JS indicates that it finished handling Keyboard animation +- (void)disableAnimation:(CDVInvokedUrlCommand*)command +{ + _shouldAnimateWebView = NO; +} + #pragma mark dealloc - (void)dealloc diff --git a/www/keyboard.js b/www/keyboard.js index d701da0..df5d0e6 100644 --- a/www/keyboard.js +++ b/www/keyboard.js @@ -22,7 +22,7 @@ var argscheck = require('cordova/argscheck'), utils = require('cordova/utils'), exec = require('cordova/exec'); - + var Keyboard = function() { }; @@ -55,7 +55,7 @@ Keyboard.fireOnShow = function() { cordova.fireWindowEvent('keyboardDidShow'); if(Keyboard.onshow) { - Keyboard.onshow(); + Keyboard.onshow(); } }; @@ -64,7 +64,16 @@ Keyboard.fireOnHide = function() { cordova.fireWindowEvent('keyboardDidHide'); if(Keyboard.onhide) { - Keyboard.onhide(); + Keyboard.onhide(); + } +}; + +Keyboard.setKeyboardAnimator = function(animator) { + Keyboard.onKeyboardAnimate = animator; + if(typeof Keyboard.onKeyboardAnimate === "function") { + exec(null, null, "Keyboard", "enableAnimation", []); + } else { + exec(null, null, "Keyboard", "disableAnimation", []); } }; @@ -80,10 +89,7 @@ Keyboard.fireOnHiding = function() { cordova.fireWindowEvent('keyboardWillHide'); if(Keyboard.onhiding) { - Keyboard.onhiding(); - } - if(Keyboard.onKeyboardAnimate) { - animationStart(); + Keyboard.onhiding(); } }; @@ -91,10 +97,7 @@ Keyboard.fireOnShowing = function() { cordova.fireWindowEvent('keyboardWillShow'); if(Keyboard.onshowing) { - Keyboard.onshowing(); - } - if(Keyboard.onKeyboardAnimate) { - animationStart(); + Keyboard.onshowing(); } }; @@ -106,10 +109,6 @@ Keyboard.hide = function() { exec(null, null, "Keyboard", "hide", []); }; -var animationStart = function() { - exec(null, null, "Keyboard", "animationStart", []); -}; - var animationComplete = function() { exec(null, null, "Keyboard", "animationComplete", []); }; @@ -117,8 +116,6 @@ var animationComplete = function() { Keyboard.beginAnimation = function(from, to, duration) { if(typeof Keyboard.onKeyboardAnimate === 'function') { Keyboard.onKeyboardAnimate(from, to, duration, animationComplete); - } else { - animationComplete(); } }; From 166d194a5d7148a2d4df8630dfc9c4624c51f2a3 Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Tue, 16 Jan 2018 14:16:15 +0100 Subject: [PATCH 7/8] Updated header --- src/ios/CDVKeyboard.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ios/CDVKeyboard.h b/src/ios/CDVKeyboard.h index a3c7dc3..5771cec 100644 --- a/src/ios/CDVKeyboard.h +++ b/src/ios/CDVKeyboard.h @@ -35,7 +35,8 @@ - (void)disableScrollingInShrinkView:(CDVInvokedUrlCommand*)command; - (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command; - (void)hide:(CDVInvokedUrlCommand*)command; -- (void)animationStart:(CDVInvokedUrlCommand *)command; - (void)animationComplete:(CDVInvokedUrlCommand *)command; +- (void)enableAnimation:(CDVInvokedUrlCommand *)command; +- (void)disableAnimation:(CDVInvokedUrlCommand *)command; @end From 254d65850bb0449ba7418222e573fffdd067453b Mon Sep 17 00:00:00 2001 From: Knut Eirik Leira Hjelle Date: Tue, 16 Jan 2018 14:18:53 +0100 Subject: [PATCH 8/8] Updated readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c50fe21..f1fe717 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ This plugin was based on this Apache [project](https://github.com/apache/cordova - [Keyboard.disableScrollingInShrinkView](#keyboarddisablescrollinginshrinkview) - [Keyboard.hide](#keyboardhide) - [Keyboard.show](#keyboardshow) + - [Keyboard.setKeyboardAnimator](#keyboardsetkeyboardanimator) - [Properties](#properties) - [Keyboard.isVisible](#keyboardisvisible) - [Keyboard.automaticScrollToTopOnHiding](#keyboardautomaticscrolltotoponhiding) - - [Keyboard.onKeyboardAnimate](#keyboardonkeyboardanimate) - [Events](#events) - [keyboardDidShow](#keyboarddidshow) - [keyboardDidHide](#keyboarddidhide) @@ -177,11 +177,11 @@ after keyboard is hiding. - iOS -## Keyboard.onKeyboardAnimate +## Keyboard.setKeyboardAnimator Assign a function that will handle content animation in the browser - Keyboard.onKeyboardAnimate = function(fromHeight, toHeight, animationDurationInMs, animationCompleteCallback) + Keyboard.setKeyboardAnimator(function(fromHeight, toHeight, animationDurationInMs, animationCompleteCallback) { // Example with jQuery (http://jquery.com/) and Velocity.JS (http://julian.com/research/velocity/) $('body').velocity({height: [to, from]}, {duration: animationDurationInMS, easing: "easeOutQuad", complete: function() @@ -189,7 +189,7 @@ Assign a function that will handle content animation in the browser // Tells the plugin that client side has finished animation animationCompleteCallback(); }}); - }; + }); #### Description