【发布时间】:2015-03-11 11:56:53
【问题描述】:
我正在尝试在键盘出现时平滑地缩小 web 视图,但我在动画方面遇到了问题。一旦动画被创建,webview 的内容就会收缩到它的新框架。在动画延迟之后,webview 本身会正确地动画到它的新大小。
下面的屏幕截图显示了它的外观。我已将 Web 视图的滚动视图的背景设置为黄色,并将视图控制器视图的背景设置为绿色。
如何使用 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];
}
感谢您的帮助
这是我正在使用的 cordova 示例应用的精简版。
* {
-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>
【问题讨论】:
-
最好只为
scrollView.contentInset属性设置动画,而不是整个帧...我不明白你为什么需要delay(可以是0.0),您可以使用键盘动画的持续时间作为动画的持续时间,当前为0.25(但您可以从键盘出现时发布的通知中读取当前值)。 -
@holex 我只是使用那些较大的值,因此问题更加明显。我刚刚尝试使用 contentInset 属性。在 7.1 上也有类似的问题。内容立即缩小,留下空白的空白区域,然后滚动视图的 contentInset 随动画一起缩小。在 8.1 上它根本没有动画。
-
@connor 有一些解决方案。你在使用自动布局吗?故事板还是一切都是程序化的?
-
@Firo 我相信它是以编程方式完成的。我在cordova中工作,所以我自己没有实现视图控制器。
标签: ios cocoa-touch uiwebview core-animation