【问题标题】:iOS PDFKit Disable vertical scroll bounceiOS PDFKit 禁用垂直滚动反弹
【发布时间】:2019-08-11 08:34:51
【问题描述】:

如何使用 PDFKitPDFView 中禁用滚动反弹?

显示 PDF 的视图没有滚动反弹选项。

这是我的代码:

if let path = Bundle.main.path(forResource: pdfObject, ofType: "pdf") {
    let url = URL(fileURLWithPath: path)
    if let pdfDocument = PDFDocument(url: url) {

        pdfView.autoresizesSubviews = true
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight,
                                    .flexibleTopMargin, .flexibleLeftMargin]

        pdfView.autoScales = true
        pdfView.displaysPageBreaks = true
        pdfView.displayDirection = .vertical
        pdfView.displayMode = .singlePageContinuous
        pdfView.document = pdfDocument

        pdfView.maxScaleFactor = 4.0
        pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit        
    }
}

在此先感谢(这可能是一个非常简单的问题!)

【问题讨论】:

    标签: ios swift scroll uiscrollview pdfkit


    【解决方案1】:

    很遗憾,没有导出 API 来设置 PDFView 所需的弹跳行为。

    话虽如此,您现在可以(安全地)利用PDFView 实现细节来破解它:

    extension PDFView {
    
        /// Disables the PDFView default bouncing behavior.
        func disableBouncing() {
            for subview in subviews {
                if let scrollView = subview as? UIScrollView {
                    scrollView.bounces = false
                    return
                }
            }
            print("PDFView.disableBouncing: FAILED!")
        }
    }
    
    

    然后在你的代码中像这样使用它:

    pdfView.disableBouncing()
    

    警告。请记住,此类解决方案可能在未来的 iOS 版本中中断。不过,请放心,您的应用不会因此而崩溃(您只是不会完全禁用弹跳行为)。

    【讨论】:

    • 您好 Paulo,感谢您的回复。不幸的是,这没有任何效果。但是,感谢您让我知道我没有错过“反弹”行为属性。可能需要等待苹果的 PDFKit 更新。
    • @NicholasFarmer 嗯...我在这里做了一个快速测试,能够成功禁用PDFView 上的弹跳行为。有机会会上传一个简短的要点...
    • Hey @NicholasFarmer, run this code in a Swift Playground 在 Xcode 中(确保 Live View 正在显示),你会看到 vertical scroll bouncing 是确实是残疾人。注释掉 pdfView.disableBouncing() 行以重新启用它。
    • 嗨,保罗,很抱歉回复晚了。我回去重新测试,但仍然没有工作......但是在最近更新并升级到 swift 5 之后 - 现在效果很好。非常感谢您的意见。您的答案已被接受并投票。亲切的问候
    • 嘿@NicholasFarmer,很高兴听到这个消息;)祝您的应用好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2019-03-07
    • 2011-11-01
    • 1970-01-01
    • 2019-11-29
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多