Подтвердить что ты не робот

Как я могу оживить сжатие uiwebview?

Я пытаюсь сгладить веб-просмотр плавно, когда появляется клавиатура, но у меня проблемы с анимацией. Как только анимация будет создана, содержимое веб-просмотра сжимается до нового фрейма. После задержки анимации веб-представление само по себе оживляет свой новый размер.

Скриншоты ниже показывают, как это выглядит. Я установил фон прокрутки просмотра веб-представления на желтый, а фон представления контроллера просмотра на зеленый.

1234

Как я могу сгладить контент с помощью webView? Это код, который у меня есть для анимации.

-(void)shrinkWebView {
    __weak CDVPlugin* weakSelf = self;
    CGRect frame = weakSelf.webView.frame;
    frame.size.height -= 400;
    [UIView animateWithDuration:2.0
                          delay:3.0
                        options:0
                     animations:^{
                         weakSelf.webView.frame = frame;
                     }
                     completion:nil];
}

Спасибо за помощь

Вот урезанная версия примера приложения кордовы, с которой я работаю.

* {
    -webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}

body {
    -webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
    -webkit-text-size-adjust: none;             /* prevent webkit from resizing text to fit */
    -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */
    background-color:#E4E4E4;
    background-image:linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image:-webkit-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image:-ms-linear-gradient(top, #A7A7A7 0%, #E4E4E4 51%);
    background-image:-webkit-gradient(
        linear,
        left top,
        left bottom,
        color-stop(0, #A7A7A7),
        color-stop(0.51, #E4E4E4)
    );
    background-attachment:fixed;
    font-family:'HelveticaNeue-Light', 'HelveticaNeue', Helvetica, Arial, sans-serif;
    font-size:12px;
    height:100%;
    margin:0px;
    padding:0px;
    text-transform:uppercase;
    width:100%;
}

/* Portrait layout (default) */
.app {
    background:url(../img/logo.png) no-repeat center top; /* 170px x 200px */
    position:absolute;             /* position in the center of the screen */
    left:50%;
    top:50%;
    height:50px;                   /* text area height */
    width:225px;                   /* text area width */
    text-align:center;
    padding:180px 0px 0px 0px;     /* image height is 200px (bottom 20px are overlapped with text) */
    margin:-115px 0px 0px -112px;  /* offset vertical: half of image height and text area height */
                                   /* offset horizontal: half of text area width */
}

/* Landscape layout (with min-width) */
@media screen and (min-aspect-ratio: 1/1) and (min-width:400px) {
    .app {
        background-position:left center;
        padding:75px 0px 75px 170px;  /* padding-top + padding-bottom + text area = image height */
        margin:-90px 0px 0px -198px;  /* offset vertical: half of image height */
                                      /* offset horizontal: half of image width and text area width */
    }
}

h1 {
    font-size:24px;
    font-weight:normal;
    margin:0px;
    overflow:visible;
    padding:0px;
    text-align:center;
}

.event {
    border-radius:4px;
    -webkit-border-radius:4px;
    color:#FFFFFF;
    font-size:12px;
    margin:0px 30px;
    padding:2px 0px;
}

.event.listening {
    background-color:#333333;
    display:block;
}

.event.received {
    background-color:#4B946A;
    display:none;
}

@keyframes fade {
    from { opacity: 1.0; }
    50% { opacity: 0.4; }
    to { opacity: 1.0; }
}
 
@-webkit-keyframes fade {
    from { opacity: 1.0; }
    50% { opacity: 0.4; }
    to { opacity: 1.0; }
}
 
.blink {
    animation:fade 3000ms infinite;
    -webkit-animation:fade 3000ms infinite;
}
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <button onclick="Keyboard.hideFormAccessoryBar(false)">Shrink</button>
            <input type="text" />
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>
4b9b3361

Ответ 1

Как насчет изменения размера document.body.style.height через javascript?

- (void)resizeWebView:(UIWebView *)webView toHeight:(CGFloat)height duration:(CGFloat)duration {
    // We will wrap all webView contents with div and animate it size
    // so webView content will resize independently of webView frame.
    NSString *javascriptString = [NSString stringWithFormat:@""
            "function animate(elem, style, unit, from, to, time) {\n"
            "    var start = new Date().getTime(),\n"
            "        timer = setInterval(function() {\n"
            "            var step = Math.min(1,(new Date().getTime()-start)/time);\n"
            "            elem.style[style] = (from+step*(to-from))+unit;\n"
            "            if( step == 1) clearInterval(timer);\n"
            "        }, 25);\n"
            "    elem.style[style] = from+unit;\n"
            "}\n"
            "document.body.style.position = 'relative';\n"
            "animate(document.body, 'height', 'px', document.body.offsetHeight, %f, %f)",
        height, duration*1000];
    [webView stringByEvaluatingJavaScriptFromString:javascriptString];

    BOOL isGrowing = height > webView.frame.size.height;

    // If webView is growing, change it frame imediately, so it content not clipped during animation
    if (isGrowing) {
        CGRect frame = webView.frame;
        frame.size.height = height;
        webView.frame = frame;
    }

    __weak typeof(webView) weakWebView = webView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        __strong typeof(weakWebView) webView = weakWebView;
        // If webview was shrinking, change it frame after animation is complete
        if (!isGrowing) {
            CGRect frame = webView.frame;
            frame.size.height = height;
            webView.frame = frame;
        }
        // Remove remove body position constraints
        [webView stringByEvaluatingJavaScriptFromString:@""
                "document.body.style.position = document.body.style.height = null;"
        ];
    });
}

- (void)toggleButtonTapped:(id)sender {
     [self resizeWebView:self.webView toHeight:300 duration:0.4];
}

Example

Изменить: Обновленный код, теперь весь html завернут с помощью <div style="position:relative;">, поэтому абсолютно позиционированные элементы также анимируются.

Редактировать 2: Хорошо, структура DOM с манипуляциями не была хорошей идеей - если вы пытались изменить размер веб-браузера, когда пользователь фокусирует ввод внутри webView и клавиатуры, входной сигнал теряет фокус, а клавиатура сразу же скрывается, Поэтому теперь я просто добавляю document.body.style.position = 'relative' для кодирования и изменения размера тела.

Я также создал категорию в UIWebView, вы можете установить его через cocoapods.

Ответ 2

Вот пример кода.

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewBottomConstraint;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Keyboard Notification
-(void)keyBoardShow:(NSNotification*)noti
{

    CGRect keyboardBounds;
    [[noti.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    [UIView animateWithDuration:duration.floatValue animations:^{
        [self.webViewBottomConstraint setConstant:keyboardBounds.size.height];
        [self.view layoutIfNeeded];
    }];

}

-(void)keyBoardHide:(NSNotification*)noti
{

    CGRect keyboardBounds;
    [[noti.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    [UIView animateWithDuration:duration.floatValue animations:^{
        [self.webViewBottomConstraint setConstant:0];
        [self.view layoutIfNeeded];
    }];

}

@end

Надеюсь, это поможет вам.

Ответ 3

Я считаю, что ответ может быть в документации Apple:

Листинг 5-1 Обработка уведомлений клавиатуры

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1

В принципе подпишитесь на внешний вид клавиатуры, чтобы сделать анимацию самостоятельно.