How to disable zoom in and zoom out in pdfView?

蹲街弑〆低调 提交于 2019-12-06 11:38:09

The solution is to create a CustomPDFView and override it's Instance Property canZoomIn :

class CustomPDFView: PDFView {
    override func canZoomIn() -> Bool {
        return false
    }
}

Then the @IBOutlet weak var pdfView: PDFView! should be of type @IBOutlet weak var pdfView: CustomPDFView!. Also update it on IdentyInspector so it will not crash you.

For more informmation please check: canZoomIn from Apple Docs

canZoomIn

Returns a Boolean value indicating whether the user can magnify the view and zoom in.

Declaration

var canZoomIn: Bool { get }

I attempted various methods of disabling pinch zoom, including overriding canZoomIn (I found it was not called on pinch zoom), setting the delegate's pdfViewWillChangeScaleFactor method (I found it was never called), and setting the max and min scale factor on the view (this prevented the view from autoscaling).

In the end, I was able to accomplish what I wanted to by adding my own pinchGestureRecognizer to the PDFView to override the view's built in pinchGestureRecognizer. In order for your pinch recognizer to have precedence, you will need to create a delegate for your recognizer and override the following method:

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
         shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        //Prefer this pinch recognizer over the default one to override pinch zoom
        if gestureRecognizer is UIPinchGestureRecognizer && otherGestureRecognizer is UIPinchGestureRecognizer {
            return true
        }
    }
   return false
}

Use

 pdfView.minScaleFactor = pdfView.scaleFactor
 pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit

And add

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    self.pdfView.autoScales = true
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!