【问题标题】:How I can move/drag multiple views on pdf view?如何在 pdf 视图上移动/拖动多个视图?
【发布时间】:2021-02-03 06:24:14
【问题描述】:

我正在做的是,我查看了 pdf 视图,它包含一个示例 pdf。 最重要的是,当用户单击导航栏中的添加按钮时,我添加了超过 1 个签署者(自定义视图)视图。

场景 1:当在 pdf 上添加第一个签署人视图 (customview) 时,它正在添加,我可以在 pdf 上拖动/移动第一个签署人视图 (Signatory1),这工作正常。

场景 2:当在 pdf 上添加第二个签署人视图 (Signatory2 customview) 时,它正在添加,我可以在 pdf 上拖动/移动第二个签署人视图 (Signatory2),这也可以正常工作,但在这种情况下,我无法移动/拖动第一个签名者视图 (Signatory1)

场景 3:类似地,当在 pdf 上添加第三个 Signatory 视图 (Signatory3 customview) 时,它正在添加,我可以在 pdf 上拖动/移动第三个 Signatory 视图 (Signatory3),这也可以正常工作,但在这种情况下,我无法移动/拖动第一个签署人视图 (Signatory1) 和第二个签署人视图 (Signatory2) 等等

问题是,我只能访问当前的签署人(我只能移动/拖动最近添加的当前签署人视图),我无法移动/拖动旧的签署人视图。

当我单击/触摸(拖动/移动)pdf 视图上的任何特定签署人视图时,如何根据我的选择移动/拖动任何签署人?

这是一些代码,

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        customView1 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
        customView2 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
        customView3 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
        customView4 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
        customView5 = SignatoryXibView(frame: CGRect(x: 30, y: 30, width: 112, height: 58))
        
        loadPdf()
    
    }
    
    func loadPdf(){
        
        if let path = Bundle.main.path(forResource: "appointment-letter", ofType: "pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfView.displayMode =  .singlePage // .singlePage //.singlePageContinuous //.twoUp
                //by default display mode is - singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical // .horizontal//.vertical
                pdfView.document = pdfDocument
            
                pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin]
                pdfView.zoomIn(self)
                pdfView.autoScales = true
                pdfView.backgroundColor = UIColor.white

                //Imp line
                pdfView.usePageViewController(true, withViewOptions: [:])
                
                currentPageNumberOfPdf = self.pdfView.currentPage?.pageRef?.pageNumber ?? 0
                print("Total Pages in PDF : ",pdfDocument.pageCount);
                
                self.pdfView.bringSubviewToFront(customView1!)
            }
        }
        
    }

 @IBAction func addSignatoryButtonClicked(_ sender: Any) {
        
        signatoryCount = signatoryCount + 1
        
        if signatoryCount == 1 {
            customView1?.signatoryLabel.text = "Signatory \(signatoryCount)"
            self.pdfView.addSubview(customView1!)
        }
        else  if signatoryCount == 2 {
            customView2?.signatoryLabel.text = "Signatory \(signatoryCount)"
            self.pdfView.addSubview(customView2!)
        }
        else  if signatoryCount == 3 {
            customView3?.signatoryLabel.text = "Signatory \(signatoryCount)"
            self.pdfView.addSubview(customView3!)
        }
        else  if signatoryCount == 4 {
            customView4?.signatoryLabel.text = "Signatory \(signatoryCount)"
            self.pdfView.addSubview(customView4!)
        }
        else  if signatoryCount == 5 {
            customView5?.signatoryLabel.text = "Signatory \(signatoryCount)"
            self.pdfView.addSubview(customView5!)
        }
    }
    

    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        let touch = touches.first
        let touchLocation = touch?.location(in: self.pdfView)
       // customView1?.center =  touchLocation!
       
        
        if signatoryCount == 1 {
            customView1?.center = touchLocation!
            return
        }
        else  if signatoryCount == 2 {
            customView2?.center = touchLocation!
            return
        }
        else  if signatoryCount == 3 {
            customView3?.center = touchLocation!
            return
        }
        else  if signatoryCount == 4 {
            customView4?.center = touchLocation!
            return
        }
        else  if signatoryCount == 5 {
           customView5?.center = touchLocation!
           return
        }
    
       // frame = view.convert(customView1!.frame, from: pdfView)
       // print("touchesMoved \(frame!.dictionaryRepresentation)")
    
    }

Here is the complete project source code

【问题讨论】:

    标签: ios swift pdf pdfview ios-pdfkit


    【解决方案1】:

    你已经很接近了。您只需要应用我在last question 中建议的方法,但要尊重超级视图的框架。

    首先将这些助手添加到您的项目中:


    extension  CGPoint {
        static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint {
            .init(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
        }
        static func +=(lhs: inout CGPoint, rhs: CGPoint) {
            lhs.x += rhs.x
            lhs.y += rhs.y
        }
    }
    

    extension UIView {
        func translate(_ translation: CGPoint) {
            let destination = center + translation
            let minX = frame.width/2
            let minY = frame.height/2
            let maxX = superview!.frame.width-minX
            let maxY = superview!.frame.height-minY
            center = CGPoint(
                x: min(maxX, max(minX, destination.x)),
                y: min(maxY ,max(minY, destination.y)))
        }
    }
    

    其次,只需摆脱平移手势识别器和 ViewController 中的对应方法。

    第三次将您的 SignatoryXibView 平移手势更改为下面的手势。这将平移视图的中心并尊重其父视图的框架:

    @objc func pan(_ gesture: UIPanGestureRecognizer) {
        translate(gesture.translation(in: self))
        gesture.setTranslation(.zero, in: self)
        setNeedsDisplay()
    }
    

    sample project

    【讨论】:

    • 非常好,它的工作并解决了边界问题并在 pdf 视图上移动多个视图。 @Leo 非常感谢您节省了我很多时间,我正在尝试记录时间。
    • @NikitaPatil 您需要的是视图中心。框架始终相同。
    • 不需要添加注释而不是视图吗?您需要将其添加到 pdf 的当前页面。添加注释后,它将随文档页面一起滚动。我认为这不会像处理视图那么容易。
    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 2011-12-15
    相关资源
    最近更新 更多