- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我写了一些代码来保存 WKWebView 的 ScrollView 的 contentOffset
和 zoomScale
,因此它们可以在 webView 加载后恢复。我发现设置这些 scrollView 属性只能使用延迟(例如使用 DispatchQueue.main.asyncAfter()
)。为什么这是必要的?有没有更好的方法来实现这个?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var contentOffset = CGPoint.zero
var zoomScale: CGFloat = 1.0
lazy var webView: WKWebView = {
let wv = WKWebView(frame: CGRect.zero)
wv.translatesAutoresizingMaskIntoConstraints = false
wv.allowsBackForwardNavigationGestures = true
wv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
wv.navigationDelegate = self
return wv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reload()
}
@IBAction func refreshTapped(_ sender: UIBarButtonItem) {
reload()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Without some delay, the restoration doesn't happen!
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
self.webView.scrollView.setZoomScale(self.zoomScale, animated: false)
self.webView.scrollView.setContentOffset(self.contentOffset, animated: false)
})
}
private func reload() {
contentOffset = webView.scrollView.contentOffset
zoomScale = webView.scrollView.zoomScale;
webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
}
}
根据 AJ B 建议更新(抱歉重复)
import UIKit
import WebKit
class ViewController: UIViewController {
private static let keyPath = "webView.scrollView.contentSize"
private static var kvoContext = 0
private var contentOffset: CGPoint?
private var zoomScale: CGFloat?
private var lastContentSize: CGSize?
private var repeatedSizeCount = 0
lazy var webView: WKWebView = {
let wv = WKWebView(frame: CGRect.zero)
wv.translatesAutoresizingMaskIntoConstraints = false
wv.allowsBackForwardNavigationGestures = true
wv.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return wv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addObserver(self, forKeyPath: ViewController.keyPath, options: .new, context: &ViewController.kvoContext)
reload()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
removeObserver(self, forKeyPath: ViewController.keyPath)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let newSize = change?[.newKey] as? CGSize, context == &ViewController.kvoContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
print("Observed: \(NSStringFromCGSize(newSize))")
// Nothing to restore if these are nil
guard let offset = contentOffset, let zoom = zoomScale else { return }
guard let lastSize = lastContentSize, lastSize == newSize else {
print("Waiting for size to settle")
lastContentSize = newSize
return
}
repeatedSizeCount += 1
guard repeatedSizeCount >= 4 else {
print("Size repeated \(repeatedSizeCount) time(s)")
return
}
print("Settled - set saved zoom and offset")
contentOffset = nil
zoomScale = nil
lastContentSize = nil
repeatedSizeCount = 0
webView.scrollView.setZoomScale(zoom, animated: false)
webView.scrollView.setContentOffset(offset, animated: false)
}
@IBAction func refreshTapped(_ sender: UIBarButtonItem) {
contentOffset = webView.scrollView.contentOffset
zoomScale = webView.scrollView.zoomScale;
reload()
}
private func reload() {
print("Reload: \(NSStringFromCGSize(webView.scrollView.contentSize))")
webView.load(URLRequest(url: URL(string: "https://www.google.com")!))
}
}
打印以下内容:
Reload: {781, 1453}
Observed: {781, 1453}
Waiting for size to settle
Observed: {320, 595}
Waiting for size to settle
Observed: {320, 595}
Size repeated 1 time(s)
Observed: {320, 595}
Size repeated 2 time(s)
Observed: {320, 595}
Size repeated 3 time(s)
Observed: {320, 595}
Settled - set saved zoom and offset
Observed: {781, 1453}
Observed: {781, 1453}
Observed: {781, 1453}
最佳答案
我正在做类似的事情,试图在加载后获取内容高度,然后将 webview 的容器调整为该高度。我为此苦苦挣扎了一段时间,我发现最好的方法是观察 WKWebView 的 ScrollView 内容高度,当内容高度以相同的大小重复出现时,您就知道它已完全加载。这有点老套,但对我来说一直有效。我想知道是否有人也知道更好的解决方案。
//This was an example html string that would demonstrate the issue
var html = "<html><body><p>We're no strangers to love You know the rules and so do I A full commitment's what I'm thinking of You wouldn't get this from any other guy I just want to tell you how I'm feeling Gotta make you understand Never gonna give you up, never gonna let you down Never gonna run around and desert you Never gonna make you cry, never gonna say goodbye Never gonna tell a lie and hurt you We've known each other for so long Your heart's been aching but you're too shy to say it Inside we both know what's been going on We know the game and we're gonna play it And if you ask me how I'm feeling Don't tell me you're too blind to see Never gonna give you up, never gonna let you down Never gonna run around and desert you Never gonna make you cry, never gonna say goodbye Never gonna tell a lie and hurt you Never gonna give you up, never gonna let you down Never gonna run around and desert you Never gonna make you cry, never gonna say goodbye Never gonna tell a lie and hurt you We've known each other for so long Your heart's been aching but you're too shy to say it Inside we both know what's been going on We know the game and we're gonna play it I just want to tell you how I'm feeling Gotta make you understand Never gonna give you up, never gonna let you down Never gonna run around and desert you Never gonna make you cry, never gonna say goodbye Never gonna tell a lie and hurt you</p><p><img src=\"https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg\" width=\"\" height=\"\" style=\"display:block;height:auto;max-width:100%;width:100%;\"></p></body></html>"
我注入(inject)了一些 javascript 来发送准备状态和 dom 加载的事件,并打印当时的大小以及 didFinish 导航功能。这是打印的内容:
// Adjusting the container height in didFinish navigation function
started navigation
committed navigation
content height = 0.0
Javascript: Ready state change interactive | content height = 0.0
Javascript: DOM content loaded | content height = 0.0
Javascript: Ready state change complete | content height = 0.0
ended navigation content height = 0.0 (didFinish navigation)
content size observed = 1638.0
height constraint = Optional(0.0)
content size observed = 691.666666666667
height constraint = Optional(0.0)
content size observed = 1171.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
content size observed = 2772.0
height constraint = Optional(0.0)
// Adjusting the container height after content size repeats itself KVO
started navigation
committed navigation
content height = 0.0
Javascript: Ready state change interactive | content height = 0.0
Javascript: DOM content loaded | content height = 0.0
Javascript: Ready state change complete | content height = 0.0
ended navigation content height = 0.0 (didFinish navigation)
content size observed = 1638.0
height constraint = Optional(1.0)
content size observed = 691.666666666667
height constraint = Optional(1.0)
content size observed = 691.666666666667
height constraint = Optional(1.0)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
content size observed = 691.666666666667
height constraint = Optional(691.666666666667)
关于ios - WKWebView 需要在 load() 之后延迟设置其 scrollView 的 contentOffset 和 zoomScale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45677288/
我正在使用一个简单的脚本来延迟加载页面上的所有图像;图像源的路径包含在 data-src 属性中,然后放入 img 标记的实际 src 属性中。几乎大多数(?)延迟加载方法的实现都是如何工作的。 这是
我有一个具有多层 (SKNodes) 背景、游戏层、前景和 HUD 的场景,每个场景中都有多个 SKSpriteNode,用于滚动和您可以收集和点击的对象。 hud 层只有一个 SKSpriteNod
我有一个 Controller 函数来创建一些东西。调用该函数时,将运行 setInterval 来获取项目的状态。 这是服务: (function () { 'use strict';
在我的应用程序中,我播放音频直播,延迟非常重要。我正在使用 AVPlayer,但启动需要 5-6 秒,并且我需要最多 3 秒的延迟。我怎样才能更快地开始播放并减少延迟?设置一个小缓冲区就可以了?如何使
我有一个恼人的问题。我有这个简单的服务器代码(比方说): #!/usr/bin/env python3 import wsgiref.simple_server def my_func(env, st
我是 jquery deferreds 的新手。这里我有一个简单的example 。 谁能告诉我为什么在其他函数完成之前就触发完成函数(“现在是我的时间”)? 这里的人 example还创建一个延迟对
正在放置关闭 之前的标签标记相同的 sa 将它们放在 中部分并指定 defer="defer"属性? 最佳答案 是/否。 是的,因为放置 defer 标签会等到文档加载完毕后再执行。 否,因为放置
我知道Javascript没有delay(500)方法,它会延迟执行500毫秒,所以我一直试图通过使用setTimeout和setInterval来解决这个问题。 for(var i =0; i< 1
我们有一个读写主服务器和复制的从读服务器。在某些网络用例中,数据被发布并立即读取以发送回服务器。立即读取是在读取从属设备上完成的,由于延迟,数据尚未在那里更新。 我知道这可能是复制设置的一个常见问题,
我有以下 dag 设置以从 2015 年开始运行追赶。对于每个执行日期,任务实例在一分钟内完成。但是,第二天的任务仅在 5 分钟窗口内开始。例如。上午 10:00、上午 10:05、上午 10:10
当我在 WatchKit 中推送一个新 Controller 并在新 Controller 的awakeWithContext: 方法中使用 setTitle 时,它需要一秒钟左右来设置标题,直到
我将图像显示为 SVG 文件和文本。 出于某种原因,svg 图像的渲染速度比屏幕的其余部分慢,从而导致延迟,这对用户体验不利。 这种延迟正常吗?我该怎么做才能让整个屏幕同时呈现? Row( ma
我正在考虑在我的应用程序中使用 firebase 动态链接。我需要将唯一标识符从电子邮件生成的链接传递到用户应用程序中。当用户安装了应用程序时,这可以正常工作,但是,我对未安装应用程序的方式有些困惑。
您知道如何使用 JQuery 的延迟方法和一个函数来检测所有已更改的表单并将每个表单作为 Ajax 帖子提交吗? 如果我只列出大量表单提交,我可以得到同样的结果,但如果我使用... $('form.c
我需要一种方法来通过回调获取不同的脚本。这个方法工作正常: fetchScripts:function() { var _this=this; $.when( $.aj
我编写了一个 jquery 脚本,允许我淡入和淡出 div,然后重复。该代码运行良好。但是,当我尝试添加延迟(我希望 div 在淡出之前保持几秒钟)时,它无法正常工作。我尝试在代码中的几个地方添加延迟
我正在努力在延迟、带宽和吞吐量之间划清界限。 有人可以用简单的术语和简单的例子来解释我吗? 最佳答案 水比喻: 延迟 是穿过管子所需的时间。 带宽是管有多宽。 水流量为吞吐量 车辆类比: 从源到目的地
我有一个 CRM 系统,当添加联系人时,我想将他们添加到会计系统中。 我在 CRM 系统中设置了一个 Webhook,将联系人传递给 Azure 函数。 Azure 函数连接到会计系统 API 并在那
我有一个 Android AudioTrack,例如: private AudioTrack mAudioTrack; int min = AudioTrack.getMinBufferSize(sa
我正在 React 中开发一个 TODO 应用程序,并尝试构建将删除选中项目延迟 X 秒的功能,并且如果在这段时间内未选中该框,它将不会被删除。 我遇到的主要问题是当用户在同一 X 秒内检查、取消检查
我是一名优秀的程序员,十分优秀!