Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support animating webview when keyboard appears #22

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

hjellek
Copy link

@hjellek hjellek commented Apr 15, 2016

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.

@cjpearson
Copy link
Owner

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?

@hjellek
Copy link
Author

hjellek commented Apr 22, 2016

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.

@ajp8164
Copy link

ajp8164 commented Sep 29, 2016

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.

@ajarbol
Copy link

ajarbol commented Feb 8, 2017

@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...

@hjellek
Copy link
Author

hjellek commented Feb 10, 2017

@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!

@kialc
Copy link

kialc commented Jul 24, 2017

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

@bugignat
Copy link

waiting for the solution of the problem...

@Aarbel
Copy link

Aarbel commented Jan 14, 2018

@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

@hjellek
Copy link
Author

hjellek commented Jan 16, 2018

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

@Aarbel
Copy link

Aarbel commented Jan 17, 2018

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
Copy link

Aarbel commented Jan 17, 2018

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

@hjellek
Copy link
Author

hjellek commented Jan 17, 2018

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');
    }
});

@Aarbel
Copy link

Aarbel commented Jan 17, 2018

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 ?

@cjpearson
Copy link
Owner

@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
Copy link
Owner

@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.

ccorcos pushed a commit to ccorcos/cordova-plugin-keyboard that referenced this pull request Jul 24, 2018
@Aarbel
Copy link

Aarbel commented Aug 22, 2019

@cjpearson any news for merge ?

@Aarbel
Copy link

Aarbel commented Nov 30, 2019

@cjpearson @hjellek any news for this PR ?

@hjellek
Copy link
Author

hjellek commented Dec 2, 2019

@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.

@Aarbel
Copy link

Aarbel commented Dec 3, 2019

@hjellek
Doesn't look like any other solutions exist

@Aarbel
Copy link

Aarbel commented Jul 31, 2020

@cjpearson do you plan to work on it ?

@Aarbel
Copy link

Aarbel commented Oct 28, 2020

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

@BrOrlandi
Copy link

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

@Aarbel
Copy link

Aarbel commented Apr 13, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shrink view shrinks web view without animation
8 participants