0

我在互联网上关注了一些示例,并且能够使用 PDFKit 绘制 Ink 注释。现在的问题是,如果我切换按钮并选择橡皮擦,我该如何擦除我绘制的注释。我在互联网上搜索并尝试了一些示例,但没有任何效果。

这是我正在使用的代码

    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
        let page = pdfView.page(for: position, nearest: true)!
        if currentAnnotation == nil {
            let border = PDFBorder()
            border.lineWidth = width
            border.style = style
            currentPDFPage = pdfView.page(for: position, nearest: true)!

            let rect = page.bounds(for: .mediaBox)

            currentAnnotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: [
                PDFAnnotationKey.border: border,
                PDFAnnotationKey.color: color,
                PDFAnnotationKey.interiorColor: UIColor.red,
                ])
            if inkType == .eraser {
                // TO DO
            }else {
                pdfView.currentPage?.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)
        currentPDFPage = pdfView.page(for: position, nearest: true)!

        if inkType == .eraser {
            // TO DO
        }
        
        if currentAnnotation != nil {
            self.registerUndo(annotations: [self.currentAnnotation])
        }


        currentAnnotation?.add(path)
        currentAnnotation = nil
        

        state = .ended
    }
4

0 回答 0