PDFKitフレームワークを試す。usePageViewControllerを使うと、色々いらなくなる。

PEAKS「iOS 11 Programming」第10章 PDF Kitを読んでPDF Kitをお試し。
https://peaks.cc

Example:usePageViewControllerを使ってみる。色々いらなくなる。

ViewController.swift

import UIKit
import PDFKit

class ViewController: UIViewController {
    
    @IBOutlet weak var pdfView: PDFView!

    var document:PDFDocument? = nil
    var filePath:URL? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        
        pdfView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        pdfView?.displayMode = .singlePage
        pdfView?.displayDirection = .horizontal
        pdfView?.displaysPageBreaks = false
        pdfView?.autoScales = true
        pdfView?.usePageViewController(true, withViewOptions: nil)
        if filePath == nil {
            document = PDFDocument(url: Bundle.main.url(forResource: "PDFKitTest", withExtension: "pdf")!)
        }
        else {
            document = PDFDocument(url: filePath!)
        }
        pdfView?.document = document
        print(pdfView.frame)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func dispPDF() {
        if filePath == nil {
            document = PDFDocument(url: Bundle.main.url(forResource: "PDFKitTest", withExtension: "pdf")!)
        }
        else {
            document = PDFDocument(url: filePath!)
        }
        pdfView?.document = document
    }
    
}

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let viewController = app.windows[0].rootViewController as! ViewController
        viewController.filePath = url
        viewController.dispPDF()
        
        return true
    }

}

iOS PDF Kitフレームワークを試す。ファイルアプリからPDFファイルを受け取り表示。

PEAKS「iOS 11 Programming」第10章 PDF Kitを読んでPDF Kitをお試し。
https://peaks.cc

Example:ファイルアプリからPDFファイルを受け取り表示。
ファイルアプリ限定ではありません。OSの共有メニューにアプリを表示させ、ファイルを受け取る。

やること①
TargetのInfoタブのDocument Typesに受け取るファイル情報を設定。

やること②
AppDelegateに受け取り処理を追加。
ファイルパスを受け取り、ViewControllerにファイルパス設定、PDFViewに表示PDF設定。

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let viewController = app.windows[0].rootViewController as! ViewController
        viewController.filePath = url
        viewController.dispPDF()
        
        return true
    }

やること③
ViewController変更。
渡されたファイルパスを表示。
ファイルパスがnil時はデフォルトファイル表示。

import UIKit
import PDFKit

class ViewController: UIViewController {
    
    var page = 0
    var pdfView:PDFView? = nil
    var document:PDFDocument? = nil
    var filePath:URL? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        
        pdfView = PDFView(frame: view.bounds)
        pdfView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        pdfView?.displayMode = .singlePage
        pdfView?.displayDirection = .horizontal
        pdfView?.autoScales = true
        if filePath == nil {
            document = PDFDocument(url: Bundle.main.url(forResource: "PDFKitTest", withExtension: "pdf")!)
        }
        else {
            document = PDFDocument(url: filePath!)
        }
        pdfView?.document = document
        view.addSubview(pdfView!)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeLeft(sender:)))
        swipeLeft.direction = .left
        pdfView?.addGestureRecognizer(swipeLeft)
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeRight(sender:)))
        swipeRight.direction = .right
        pdfView?.addGestureRecognizer(swipeRight)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

   func dispPDF() {
        if filePath == nil {
            document = PDFDocument(url: Bundle.main.url(forResource: "PDFKitTest", withExtension: "pdf")!)
        }
        else {
            document = PDFDocument(url: filePath!)
        }
        pdfView?.document = document
    }
    
    @objc func swipeLeft(sender: UISwipeGestureRecognizer) {
        if page < (document?.pageCount)! - 1 {
            page = page + 1
            pdfView?.go(to: (document?.page(at: page))!)
        }
    }

    @objc func swipeRight(sender: UISwipeGestureRecognizer) {
        if page > 0 {
            page = page - 1
            pdfView?.go(to: (document?.page(at: page))!)
        }
    }

}

ファイル管理をファイルアプリに任せて、独自ビューアを作成することが出来ます。

iOS PDF Kitフレームワークを試す。スワイプでページめくり追加。

PEAKS「iOS 11 Programming」第10章 PDF Kitを読んでPDF Kitをお試し。
https://peaks.cc

Example:スワイプでページめくり追加

import UIKit
import PDFKit

class ViewController: UIViewController {
    
    var page = 0
    var pdfView:PDFView? = nil
    var document:PDFDocument? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        
        pdfView = PDFView(frame: view.bounds)
        pdfView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        pdfView?.displayMode = .singlePage
        pdfView?.displayDirection = .horizontal
        pdfView?.autoScales = true
        document = PDFDocument(url: Bundle.main.url(forResource: "iOS11_Programming_v1.0", withExtension: "pdf")!)
        pdfView?.document = document
        view.addSubview(pdfView!)
        
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeLeft(sender:)))
        swipeLeft.direction = .left
        pdfView?.addGestureRecognizer(swipeLeft)
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeRight(sender:)))
        swipeRight.direction = .right
        pdfView?.addGestureRecognizer(swipeRight)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @objc func swipeLeft(sender: UISwipeGestureRecognizer) {
        if page < (document?.pageCount)! - 1 {
            page = page + 1
            pdfView?.go(to: (document?.page(at: page))!)
        }
    }

    @objc func swipeRight(sender: UISwipeGestureRecognizer) {
        if page > 0 {
            page = page - 1
            pdfView?.go(to: (document?.page(at: page))!)
        }
    }

}

iOS PDF Kitフレームワークを試す。

PEAKS「iOS 11 Programming」第10章 PDF Kitを読んでPDF Kitをお試し。
https://peaks.cc

PDF KitがあるとPDF Viewerを簡易に開発出来ます。
便利なフレームワークだ。

Apple Document
https://developer.apple.com/documentation/pdfkit

Example:PDFファイルの11頁目を表示

import UIKit
import PDFKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let pdfView = PDFView(frame: view.bounds)
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        pdfView.displayMode = .singlePage
        pdfView.displayDirection = .horizontal
        pdfView.autoScales = true
        let document = PDFDocument(url: Bundle.main.url(forResource: "iOS11_Programming_v1.0", withExtension: "pdf")!)
        pdfView.document = document
        view.addSubview(pdfView)
        pdfView.go(to: (document?.page(at: 10))!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

頁移動はちょっと癖があります。
下記はPDFPageクラスを指定する方法。
PDFDocumentクラスのpage()メソッドでPDFPageクラスのインスタンスが取得出来ます。

pdfView.go(to: (document?.page(at: 10))!)

ページ指定は0オリジンです。

独自のPDFViewerを作成するか。。。

iOSのタブ内を画像のみにする。画像の位置を調整。

class FirstViewController: UIViewController {
   @IBOutlet weak var tabItem: UITabBarItem!
    override func viewDidLoad() {
        super.viewDidLoad()

        var insets = UIEdgeInsets()
        insets.top = 5
        insets.bottom = -5.0
        tabItem.imageInsets = insets
    }
}