我已成功将 pdf 文件从 URL 加载到 Web View 。我想在触摸 webview 后获取坐标,将其数据发送到服务器,以在服务器端的该位置添加一些文本。问题是当 webview 缩放或滚动到第 2,3 页时如何获取该坐标.. 你能帮我解决 Swift 3 中的问题
这里是代码
class ViewController: UIViewController, UITextFieldDelegate, UIGestureRecognizerDelegate, UIWebViewDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.pdf995.com/samples/pdf.pdf")!
webView.loadRequest(URLRequest(url: url))
let webViewTapped = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
webViewTapped.numberOfTouchesRequired = 1
webViewTapped.delegate = self
webView.addGestureRecognizer(webViewTapped)
}
func tapAction(_ sender: UITapGestureRecognizer) {
// This always return the same coordinates in zoom or not zoom
let point = sender.location(in: webView)
print("======")
print("x: \(point.x) y: \(point.y)")
}
PS:我是 swift 的新手
最佳答案
用户3783161,
首先,如果您不使用 UIGestureRecognizerDelegate 中的任何方法,则可以删除以下行。
webViewTapped.delegate = self
要在触摸 UIWebView 时获取实际坐标,您需要在 UIScrollView 上获取位置,而不是从 UIWebView 获取位置。
func tapAction(_ sender: UITapGestureRecognizer) {
// This always return the same coordinates in zoom or not zoom
// let point = sender.location(in: webView)
// print("======WebView")
// print("x: \(point.x) y: \(point.y)")
// Get location frown webView.ScrollView
let point = sender.location(in: webView.scrollView)
print("======ScrollView")
print("x: \(point.x) y: \(point.y)")
}
但是你仍然有一个问题,因为该位置是相对于 UIWebView 的内容大小而不是 PDF 文档。 我没有找到这个问题的答案,但你可以在以下之间进行简单的转换:
PDF 纸张大小与 WebView 大小
由于 PDF 必须包含此信息(pdf 可以是 A4/A3/A5/C1/等)。
关于ios - 接触 Swift 3 时在 Webview 中获取 pdf 的实际坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072786/