cordova-plugin-keyboard icon indicating copy to clipboard operation
cordova-plugin-keyboard copied to clipboard

Support animating webview when keyboard appears

Open hjellek opened this issue 9 years ago • 23 comments

An attempt to fix #13 for iOS so that animation is possible while the keyboard appears and disappears.

The approach is based on the project https://github.com/glyuck/GLKAnimateWebViewFrame with some modifications, and the only issue for me so far is that it seems like the webview content freezes for most of the time while animating the keyboard in, and then plays the whole animation. When the keyboard animates out the webview content seems to be able to animate as expected.

Comments and suggestions are appreciated.

hjellek avatar Apr 15 '16 07:04 hjellek

Thanks for the PR. This issue has bugged me for a while. I like the approach of letting the user define their own JS animation. The reason I didn't include GLKAnimateWebViewFrame was because the animation may not work for everything.

Have you tried using this with WKWebView?

cjpearson avatar Apr 21 '16 23:04 cjpearson

Yeah, I've spent some time trying to figure out a way that works as well. So far, this has been the closest to a good solution, even though it bugs me that the content seems to freeze while the keyboard animates in.

I have not tried it with WKWebView yet. Once I am back from vacation I can look into setting up a demo with it.

hjellek avatar Apr 22 '16 12:04 hjellek

Does this enhancement work and will it be accepted. I worked around it by adding a transition animation to by content that closely resembles the native transition (ease in/out). It works fine but it would be better to have it done by this plugin.

ajp8164 avatar Sep 29 '16 14:09 ajp8164

@hjellek did you ever get this to animate with the keyboard onOpen? I've looked into it quite a bit, but never could figure out why the animations are queued on the focus event, but not the blur. GLKAnimateWebViewFrame is smooth, but does not actually implement the keyboard...

ajarbol avatar Feb 08 '17 13:02 ajarbol

@ajarbol unfortunately, no. I have not checked if there has been any changes in later versions of iOS that could be used either. Should perhaps look into it again, would be nice to get it working properly onOpen as well!

hjellek avatar Feb 10 '17 10:02 hjellek

Any news on a fix for this (using WKWebView)?

kialc avatar Jul 24 '17 11:07 kialc

waiting for the solution of the problem...

bugignat avatar Jul 27 '17 10:07 bugignat

@cjpearson @shazron @stevengill @hjellek when could we definitely fix this issue ?

Clean keyboard interaction is very important for Cordova ... Thanks again for this plugin, we are close of full ios hack !

Here is an other example : Keyboard open / close creates ugly glitch with our app

  • Cordova is just a wrapper for our web app, we use plugin cordova-plugin-wkwebview-engine and we use in-app browser with window.location = 'https://ourresponsiveapp.com').
  • when keyboard is Opening, i can't find the origin of the white space (it's not the <html> or the <body> of Cordova, nor the <html> or <body> of our web app wrapped by Cordova webview.
  • when keyboard is Closing you can see the <html> red background of the web app
  • i slowed down a little bit the video to clearly see what happens

cordova keyboard issue

Aarbel avatar Jan 14 '18 15:01 Aarbel

Sorry for the really late response from my side.

My branch has been updated to work with WKWebView now. Had to change the implementation slightly since the timing between JS and native code seemed to have changed since my last attempt, but I think the implementation is better now anyway.

Here are some crude screenshots from our app running on iPhone 5s (the JS implementation is not optimized whatsoever, basically the demo code from the README)

Without animation: keyboard without animation

With animation: keyboard with animation

hjellek avatar Jan 16 '18 13:01 hjellek

Great @hjellek !

Have you tried with CSS 3D Acceleration hack instead of javascript animation ? Javascript animations take a lot of CPU on a mobile, and make the animation lagging / slowing. Css 3D acceleration use the GPU of the phone which is much faster and linear, cf https://www.urbaninsight.com/article/improving-html5-app-performance-gpu-accelerated-css-transitions

An example :

body {
    height: $fromHeight;
    transform: transale3d(0,0,0);
    transition: all 1s;
}

body .keyboard-open {
    height: $toHeight;
    transform: translateZ(0);
}

Aarbel avatar Jan 17 '18 12:01 Aarbel

ps: if you implement css animation, i could spent 1 hour to make the right bezier animation that fits with the ios' keyboard animation ;)

Aarbel avatar Jan 17 '18 12:01 Aarbel

I haven't focused too much on the client side of the animation yet. Basically, the animator function receives a notification when the keyboard begins to animate, and the animationComplete callback needs to be called when the client is finished animating.

Something along the lines of this completely untested code should work for CSS transitions I think:

Keyboard.setKeyboardAnimator(function(fromHeight, toHeight, animationDurationInMs, animationCompleteCallback)
{
    var body = document.querySelector('body');
    var transitions = {
        'transition': 'transitionend',
        'OTransition': 'oTransitionEnd',
        'MozTransition': 'transitionend',
        'WebkitTransition': 'webkitTransitionEnd'
    };

    var transition = "";
    for (var t in transitions)
    {
        if (body.style[t] !== undefined)
        {
            transition = transitions[t];
            break;
        }
    }


    body.addEventListener(transition, function()
    {
        animationCompleteCallback();
    });
    if(fromHeight < toHeight)
    {
        body.classList.remove('keyboard-open');    
    }
    else 
    {
        body.classList.add('keyboard-open');
    }
});

hjellek avatar Jan 17 '18 12:01 hjellek

Ok i'll try it this afternoon !

Otherwise the webview seems to be resized by cordova-plugin-keyboard at the end of the ios keyboard animation.

One thought about this :

  • Maybe we will bypass these king of weird behaviors if we directly resize the body and not the webview when keyboard opens... Resizing webview often create glitch effects with body background, even on desktop What do you think @hjellek @cjpearson ?

Aarbel avatar Jan 17 '18 12:01 Aarbel

@hjellek, thanks for updating this PR! I like the general approach here. I was hesitant to include a JavaScript solution since it likely would not work for every app. Allowing users to define their own animation function should get around this. Also, the docs are appreciated. shrinkViewKeyboardWillChangeFrame: has had some issues in the past, so I'll want to test a few configurations first.

cjpearson avatar Jan 24 '18 23:01 cjpearson

@Aarbel, regarding your first comment I think the whitespace is either part of the app's view controller or one of the WebView's views. I don't recall which, but I explored it a while ago when I first tackled this bug. https://stackoverflow.com/q/27923648/754604

I think one modification we could make is providing a similar animator function when ShrinkView is disable. Then users could shrink the body or any other element with JavaScript. You could try adding a handler to the keyboardHeightWillChange event, but I'm not sure if that provides enough information.

cjpearson avatar Jan 24 '18 23:01 cjpearson

@cjpearson any news for merge ?

Aarbel avatar Aug 22 '19 20:08 Aarbel

@cjpearson @hjellek any news for this PR ?

Aarbel avatar Nov 30 '19 22:11 Aarbel

@Aarbel sorry, no news on my end. I have not used this in quite a while now, and am not developing anything with Cordova these days.

No idea if this still works, or even is the best solution anymore.

hjellek avatar Dec 02 '19 19:12 hjellek

@hjellek Doesn't look like any other solutions exist

Aarbel avatar Dec 03 '19 17:12 Aarbel

@cjpearson do you plan to work on it ?

Aarbel avatar Jul 31 '20 10:07 Aarbel

@cjpearson @hjellek would be really great to merge this branch, do you need something to work on it ? Freelance mission for example

Aarbel avatar Oct 28 '20 16:10 Aarbel

Any updates on this? I tested on iOS 14 and it doesn't work. Did I make something wrong and it should work?

BrOrlandi avatar Feb 22 '21 18:02 BrOrlandi

@cjpearson do you think you will maintain this part some day ?

Aarbel avatar Apr 13 '22 09:04 Aarbel