Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS(Swift)上同时使用PDFKit中的铅笔(如墨水)注释和手指触摸导航?_Ios_Swift_Pdfkit_Pdfview - Fatal编程技术网

如何在iOS(Swift)上同时使用PDFKit中的铅笔(如墨水)注释和手指触摸导航?

如何在iOS(Swift)上同时使用PDFKit中的铅笔(如墨水)注释和手指触摸导航?,ios,swift,pdfkit,pdfview,Ios,Swift,Pdfkit,Pdfview,我用Swift和PDFKit编写了一个PDF注释应用程序,它使用touchesBegind/Moved/End在PDF上进行注释。适用于绘制注释 为了获得铅笔触碰事件,如果我做注释,我需要在PDFView上设置一个切换按钮“isUserInteractionEnabled=false”,并设置“isUserInteractionEnabled=true”来浏览(平移/缩放)PDF文档。 使用“isUserInteraction=true”,所有触摸事件都会被PDFView“吃掉”(我认为是doc

我用Swift和PDFKit编写了一个PDF注释应用程序,它使用touchesBegind/Moved/End在PDF上进行注释。适用于绘制注释

为了获得铅笔触碰事件,如果我做注释,我需要在PDFView上设置一个切换按钮“isUserInteractionEnabled=false”,并设置“isUserInteractionEnabled=true”来浏览(平移/缩放)PDF文档。 使用“isUserInteraction=true”,所有触摸事件都会被PDFView“吃掉”(我认为是documentView Scrollview),并且不会在ViewController上调用

pepetual切换对用户来说真的很烦人,不可用

那么,我如何使用ViewController中的touchesBegind/Moved/End覆盖,并能够通过手指触摸在PDF中导航(平移/缩放),而无需一直切换isUserInteractionEnabled

该应用程序只能用铅笔在iPad上运行

谢谢你抽出时间, 拉尔斯

使事情更清楚的示例实现: (类内ViewController:UIViewController)

override func viewDidLoad(){
super.viewDidLoad()
pdfView=pdfView()
pdfView.translatesAutoResizengMaskintoConstraints=false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(等式: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
让url=Bundle.main.url(用于资源:“TestPDF”,扩展名为:“pdf”)!
pdfView.document=PDFDocument(url:url)
pdfView.isUserInteractionEnabled=false//Touchs事件称为…铅笔模式注释
//pdfView.isUserInteractionEnabled=true//Touchs事件从不被调用……但平移/缩放可以工作
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
打印(“触摸开始呼叫!”)
如果让触摸=先触摸{
如果touch.type==.stylus{
打印(“触摸开始铅笔注释…”)
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
打印(“触摸移动呼叫!”)
如果让触摸=先触摸{
如果touch.type==.stylus{
打印(“触摸移动铅笔批注…”)
}
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
打印(“touchesend called!”)
如果让触摸=先触摸{
如果touch.type==.stylus{
打印(“触摸式铅笔批注…”)
}
}
}

您可以在PDFView的UIgestureRecognitor中实现所有绘图操作,也可以实现此方法

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
{
    if touch.type == .stylus
    {
        return true
    }
    else
    {
        return false
    }
}

可能很晚了,但我刚刚发现自己处于完全相同的境地。所以,我找到了这个苹果的示例代码:! 最终创建了自定义手势识别器。这仍然是非常阿尔法代码,但你会理解这个想法:

class PDFDrawingGestureRecognizer: UIGestureRecognizer {
weak var pdfView: PDFView!
private var lastPoint = CGPoint()
private var currentAnnotation : PDFAnnotation?


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first,
        let numberOfTouches = event?.allTouches?.count,
        numberOfTouches == 1 {
        state = .began

        let position = touch.location(in: pdfView)
        let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

        lastPoint = convertedPoint
    } else {
        state = .failed
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    state = .changed

    guard let position = touches.first?.location(in: pdfView) else { return }
    let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

    let path = UIBezierPath()
    path.move(to: lastPoint)
    path.addLine(to: convertedPoint)
    lastPoint = convertedPoint

    if currentAnnotation == nil {
        let border = PDFBorder()
        border.lineWidth = 10
        border.style = .solid

        currentAnnotation = PDFAnnotation(bounds: pdfView.bounds, forType: .ink, withProperties: [
            PDFAnnotationKey.border: border,
            PDFAnnotationKey.color: UIColor.red,
            PDFAnnotationKey.interiorColor: UIColor.red,
            ])
        let pageIndex = pdfView.document!.index(for: pdfView.currentPage!)
        pdfView.document?.page(at: pageIndex)?.addAnnotation(currentAnnotation!)
    }
    currentAnnotation!.add(path)


}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let position = touches.first?.location(in: pdfView) else {
        state = .ended
        return
    }

    let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

    let path = UIBezierPath()
    path.move(to: lastPoint)
    path.addLine(to: convertedPoint)

    currentAnnotation?.add(path)
    currentAnnotation = nil

    state = .ended
}
}
类PDFDrawingGestureRecognitor:UIGestureRecognitor{ 弱变量pdfView:pdfView! 私有变量lastPoint=CGPoint() 私有变量currentAnnotation:PDFAnnotation? 覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){ 如果let touch=touch.first, 让NumberOfTouchs=事件?.AllTouchs?.count, NumberOfTouchs==1{ 状态=。开始 让位置=触摸位置(in:pdfView) 让convertedPoint=pdfView.convert(位置,to:pdfView.currentPage!) lastPoint=转换点 }否则{ 状态=。失败 } } 覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){ 状态=。已更改 guard let position=touch.first?.location(in:pdfView)else{return} 让convertedPoint=pdfView.convert(位置,to:pdfView.currentPage!) let path=UIBezierPath() 路径。移动(到:最后一点) path.addLine(到:convertedPoint) lastPoint=转换点 如果currentAnnotation==nil{ let border=PDFBorder() border.lineWidth=10 border.style=.solid currentAnnotation=PDFAnnotation(边界:pdfView.bounds,forType:.ink,withProperties:[ PDFAnnotationKey.border:边框, PDFAnnotationKey.color:UIColor.red, PDFAnnotationKey.interiorColor:UIColor.red, ]) 让pageIndex=pdfView.document!.index(用于:pdfView.currentPage!) pdfView.document?第页(位于:pageIndex)?.addAnnotation(当前批注!) } currentAnnotation!.add(路径) } 覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){ 防护罩位置=接触。第一?位置(在:pdfView中)其他{ 状态=。结束 返回 } 让convertedPoint=pdfView.convert(位置,to:pdfView.currentPage!) let path=UIBezierPath() 路径。移动(到:最后一点) path.addLine(到:convertedPoint) currentAnnotation?.add(路径) currentAnnotation=nil 状态=。结束 } }
最后,将此手势识别器添加到PDFView。如果您希望您的手势识别器仅使用铅笔,请在
touchesbeated
方法中检查您的触摸原点。

您可以发布您的实现吗?当然。。。我编辑了我原来的帖子。示例实现基于使用xcode 9的新iOS->单视图应用程序项目。您是如何在PDFView中实现InkAnnotation的?我在实施这一点上有问题,你能告诉我吗。。。这个想法就是我所有的绘图代码所使用的,包括墨水注释。它解决了吗?我对PDFView进行了子类化,并用你的代码片段覆盖了手势识别器。。。我可以用铅笔第一次接触(开始)。但是对于绘图代码,我需要(1)铅笔的开始,(2)移动和(3)结束点
class PDFDrawingGestureRecognizer: UIGestureRecognizer {
weak var pdfView: PDFView!
private var lastPoint = CGPoint()
private var currentAnnotation : PDFAnnotation?


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first,
        let numberOfTouches = event?.allTouches?.count,
        numberOfTouches == 1 {
        state = .began

        let position = touch.location(in: pdfView)
        let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

        lastPoint = convertedPoint
    } else {
        state = .failed
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    state = .changed

    guard let position = touches.first?.location(in: pdfView) else { return }
    let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

    let path = UIBezierPath()
    path.move(to: lastPoint)
    path.addLine(to: convertedPoint)
    lastPoint = convertedPoint

    if currentAnnotation == nil {
        let border = PDFBorder()
        border.lineWidth = 10
        border.style = .solid

        currentAnnotation = PDFAnnotation(bounds: pdfView.bounds, forType: .ink, withProperties: [
            PDFAnnotationKey.border: border,
            PDFAnnotationKey.color: UIColor.red,
            PDFAnnotationKey.interiorColor: UIColor.red,
            ])
        let pageIndex = pdfView.document!.index(for: pdfView.currentPage!)
        pdfView.document?.page(at: pageIndex)?.addAnnotation(currentAnnotation!)
    }
    currentAnnotation!.add(path)


}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let position = touches.first?.location(in: pdfView) else {
        state = .ended
        return
    }

    let convertedPoint = pdfView.convert(position, to: pdfView.currentPage!)

    let path = UIBezierPath()
    path.move(to: lastPoint)
    path.addLine(to: convertedPoint)

    currentAnnotation?.add(path)
    currentAnnotation = nil

    state = .ended
}
}