简体   繁体   中英

Swift PDFView jumps when frame is animated

I have an app that has a fullscreen PDFView, and I'm trying to add an animatable tab bar on the top of the screen. However, any time I animate the PDFView's frame to show the tab bar, the PDF loaded in the view jumps to another position.

I've tried to animate with UIView.animate and with UIViewPropertyAnimator, and adding the PDFView as a subview and animating the parent, but all result in the same problem.

Has anyone encountered this problem? Any ideas on how to create the animation in a way that would result in a smooth user experience? The tab bar could obviously animate itself on top of the PDFView, but I don't like the idea of it covering the PDF.

在此处输入图片说明

Thanks!

func CreatePDFView() {
    pdfView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    view.addSubview(pdfView)
}
func AnimatePDFView() {
    UIView.animate(withDuration: 1) {
        self.pdfView.frame = CGRect(x: 0, y: 40, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 40)
    }
}

I've been looking to add this functionality for months, but haven't been able to solve this. As soon as I asked this question, though, it seems that I did.

You need to animate pdfView.documentView?.frame, and compensate for the current frame. I did this with UIEdgeInsets, and it seems to work.

if tabDocumentBrowserIsVisible {
        tabDocumentBrowser.Animate(toState: .hidden)
        UIView.animate(withDuration: 0.3) {
            let inset = UIEdgeInsets(top: -80, left: 0, bottom: 0, right: 0)
            self.pdfView.documentView?.frame = self.pdfView.documentView?.frame.inset(by: inset) as! CGRect
        }
        tabDocumentBrowserIsVisible = false
    } else {
        tabDocumentBrowser.Animate(toState: .visible)
        UIView.animate(withDuration: 0.3) {
            let inset = UIEdgeInsets(top: 80, left: 0, bottom: 0, right: 0)
            self.pdfView.documentView?.frame = self.pdfView.documentView?.frame.inset(by: inset) as! CGRect
        }
        tabDocumentBrowserIsVisible = true
    }

The PDF still behaves a bit funny if you are scrolling it while you animate it, but it's not too bad.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM