使用Swift 3触摸时在Webview中获取pdf的实际坐标 [英] Get actual coordinate of pdf in Webview when touch with Swift 3

查看:32
本文介绍了使用Swift 3触摸时在Webview中获取pdf的实际坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将 pdf 文件从 URL 加载到 Web 视图.我想在触摸 webview 后获取坐标以将其数据发送到服务器以在服务器端的该位置添加一些文本.问题是如何在 webview 缩放或滚动到第 2,3 页时获得该坐标.你能帮我解决 Swift 3 中的问题吗

I have successfully to load a pdf file from URL to a web view. I want to get the coordinates after touch on webview to sent it data to server to add some text to that position in server side. Problem is how can I get that coordinates when webview is zooming or scroll to page 2,3.. Could you please help me to solve the problem in 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 的新手

PS: I am a newbie in swift

推荐答案

user3783161,

user3783161,

首先,如果您不使用 UIGestureRecognizerDelegate 中的任何方法,您可以删除以下行.<代码>webViewTapped.delegate = self

First of all, you can remove the following line, if you don't use any method from UIGestureRecognizerDelegate. webViewTapped.delegate = self

要获取触摸 UIWebView 时的实际坐标,您需要在 UIScrollView 上获取位置,而不是从 UIWebView 获取.

To get the the actual coordinate when you touch the UIWebView, you need to get the location on UIScrollView and not from 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 文档的.我没有找到答案,但您可以在以下之间进行简单的转换:

But you still have a problem, because the location is relative to the content size of the UIWebView and not to the PDF document. I did not find the answer to this, but you can do a simple conversion between:

PDF Paper Size vs. WebView Size

由于PDF必须有这些信息(pdf可以是A4/A3/A5/C1/等).

Since the PDF must have this information (the pdf can be A4 / A3 / A5 / C1 / etc).

这篇关于使用Swift 3触摸时在Webview中获取pdf的实际坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆