Search code examples
iosobjective-cswiftuipangesturerecognizertouchesbegan

QLPreviewController and touchesBegan method


I am working with QLPreviewController in Swift. So far I was able to display a PDF by adding it to my subview, instead of pushing it or presenting it. This allows me to have a custom toolbar at bottom of my document and a custom navigation at the top.

Now I need to be able to use the touchesBegan method for my QLPreviewController view.

I was doing some research and it turns out I need to make a subclass of QLPreviewController and override the viewDidAppear and add a UITapGestureRecognizer.

My question is, how do I make a subclass of QLPreviewController?

Does it look something like this?

import UIKit
import QuickLook

class QLPreview: QLPreviewController  {

    override func viewDidLoad()
    {
        //Do UITapGestureRecognizer
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        //Touch QLPreviewController view
    }

    func detectPan(recognizer:UIPanGestureRecognizer)
    {

    }


}

Here is how I am adding the QLPreviewController view to subview

viewPDF = QLPreviewController()

        viewPDF.dataSource = self

        viewPDF.delegate = self

        viewPDF.view.frame = CGRectMake(0, 60, 375.0, 667.0)

        self.view.addSubview(viewPDF.view)

        viewPDF.view.userInteractionEnabled = true

        self.navigationController?.toolbarHidden = false
        self.navigationController?.navigationBarHidden = false;

And Here are my delegate methods

 func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {

        PDFTitle.title = fileNameGroup

        let localUrl = String(format:"%@/%@", PDFFilePath, fileNameGroup)
        let url = NSURL.fileURLWithPath(localUrl)

        return url
    }

Any Help would be very appreciated

UPDATE

Here is what I got so far....I rewrote my subclass like so:

import UIKit
import QuickLook

class QLPreview: QLPreviewController, UIGestureRecognizerDelegate  {

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.view.frame = CGRectMake(0, 60, 375.0, 667.0)

        let panRecognizer = UIPanGestureRecognizer(target:self, action: #selector(QLPreview.detectPan))

        self.view.userInteractionEnabled = true

        self.view.addGestureRecognizer(panRecognizer)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        print("touching")
    }

    func detectPan(recognizer:UIPanGestureRecognizer)
    {
        print("detect")
    }

}

and adding it like so:

let preview = QLPreview()

        preview.delegate = self
        preview.dataSource = self

        self.view.addSubview(preview.view)

but still my print methods do not appear.


Solution

  • I don't have reputation to comment yet.

    To answer your question, you are subclassing QLPreviewController correctly. However, your print methods not working is a completely different problem. It is very difficult to get any gesture recognizers working with QLPreviewController.

    Please let me know if you get it working.