ios - 如何在iOS(Swift)上同时使用PDFKit中的铅笔(例如墨水)注释和手指触摸导航?
问题描述
我在 Swift 中使用 PDFKit 编写了一个 PDF 注释应用程序,它使用 touchesBegan/Moved/Ended 在 PDF 上进行注释。非常适合绘制注释。
为了获得铅笔触摸事件,如果我进行注释并设置“isUserInteractionEnabled = true”以导航(平移/缩放)PDF文档,我需要制作一个切换按钮以在PDFView上设置“isUserInteractionEnabled = false”。使用“isUserInteraction = true”,所有触摸事件都被 PDFView“吃掉”(我认为它是 documentView Scrollview),并且永远不会在 ViewController 上调用。
永久切换对用户来说真的很烦人并且不可用。
那么如何在 ViewController 中使用 touchesBegan/Moved/Ended 覆盖,并能够通过手指触摸在 PDF 中导航(平移/缩放)而无需一直切换 isUserInteractionEnabled?
该应用程序应仅使用铅笔在 iPad 上运行。
感谢您抽出宝贵时间,拉斯
使事情更清晰的示例实现:(在 ViewController 类中:UIViewController)
override func viewDidLoad() {
super.viewDidLoad()
pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
let url = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!
pdfView.document = PDFDocument(url: url)
pdfView.isUserInteractionEnabled = false //touches events are called ... annotation with pencil mode
//pdfView.isUserInteractionEnabled = true //touches events are NEVER called ... but pan/zoom works
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesBegan called!")
if let touch = touches.first {
if touch.type == .stylus {
print("touchesBegan pencil annotation ...")
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesMoved called!")
if let touch = touches.first {
if touch.type == .stylus {
print("touchesMoved pencil annotation ...")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touchesEnded called!")
if let touch = touches.first {
if touch.type == .stylus {
print("touchesEnded pencil annotation ...")
}
}
}
解决方案
You can implement all your drawing operations in PDFView's UIGestureRecognizer and implement this method also.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
{
if touch.type == .stylus
{
return true
}
else
{
return false
}
}
推荐阅读
- delphi - Rest数据连接期间Android Socket Timeout Exception,仅在移动网络连接期间
- time-series - 如何将python中的时间序列数据重新采样为不规则间隔
- css - 具有动态行的 Material-UI 文本字段
- jenkins - 詹金斯:docker.com 的太多请求
- azure - Azure 应用服务 - 始终开启与运行状况检查
- python - 如何使用 python 正则表达式查找标签并替换内部内容
- qt - 如果图像源不可用,有没有办法从 qml 中删除警告?
- flutter - 颤动动画控制器值未升至上限
- hyperledger-fabric - 如何在 Hyperledger Fabric 2.x 中创建自定义测试网络?如何在 test-netork 的私人频道中创建五个对等组织?
- python - 如何使用python使用多个IF条件语句