10

I have a web view that display a table much larger than the screen. When I try to scroll it diagonally AFTER zooming - both in or out, it usually only scrolls in a single direction instead of diagonally. Is this behaviour due to web view's scrollview or could I have made mistake in my codes?

This is how I populate my web view:

    webViewContent.scrollView.bounces = false
    webViewContent.scrollView.bouncesZoom = false
    webViewContent.scrollView.delegate = self
    webViewContent.scalesPageToFit = true

    var htmlString = "<html><head>... ... a really long string that creates a table"
    webViewContent.loadHTMLString(htmlString, baseURL: nil)

Please do tell me if the htmlString might affect, I did not include it because it is really long.

I also tried to synchronise the view with a header row called webViewTitle which I populate using similar codes but only one row. The synchronise codes are like:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    if(scrollView != webViewTitle.scrollView){
        var zoomPadding:CGFloat = 0.0
        if((currentZoomScale*webViewContent.scrollView.zoomScale) < 1){
            zoomPadding = 0.5*(-acos(currentZoomScale*webViewContent.scrollView.zoomScale)*180.0/CGFloat.pi)
        }else{
            zoomPadding = 0.5*acos(2-(currentZoomScale*webViewContent.scrollView.zoomScale))*180.0/CGFloat.pi
        }
        webViewTitle.scrollView.zoom(to: CGRect(x: webViewContent.scrollView.contentOffset.x,
                                                y: (355*currentZoomScale*webViewContent.scrollView.zoomScale) + zoomPadding,
                                                width: webViewTitle.scrollView.bounds.width/currentZoomScale/webViewContent.scrollView.zoomScale,
                                                height: webViewTitle.scrollView.bounds.height/currentZoomScale/webViewContent.scrollView.zoomScale),
                                     animated: false)
    }
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    var zoomPadding:CGFloat = 0.0
    if((currentZoomScale*webViewContent.scrollView.zoomScale) < 1){
        zoomPadding = 0.5*(-acos(currentZoomScale*webViewContent.scrollView.zoomScale)*180.0/CGFloat.pi)
    }else{
        zoomPadding = 0.5*acos(2-(currentZoomScale*webViewContent.scrollView.zoomScale))*180.0/CGFloat.pi
    }
    if(scrollView == webViewTitle.scrollView){
        webViewTitle.scrollView.contentOffset.y = (355*currentZoomScale*webViewContent.scrollView.zoomScale) + zoomPadding
        webViewContent.scrollView.contentOffset.x = webViewTitle.scrollView.contentOffset.x
    }else{
        webViewTitle.scrollView.contentOffset.y = (355*currentZoomScale*webViewContent.scrollView.zoomScale) + zoomPadding
        webViewTitle.scrollView.contentOffset.x = webViewContent.scrollView.contentOffset.x
    }
}

Could any of these caused the diagonal scrolling to become buggy?

1 Answer 1

-1

Could any of these caused the diagonal scrolling to become buggy?

Yes

From the provided code, you are implementing the UIScrollViewDelegate for the UIWebView, but in both shown implementations, you are not calling super. This means that the scrollView is not going to have it's standard behavior, and instead strictly use your code for it's scrollViewDidScroll and scrollViewDidZoom behavior.

While this question doesn't necessarily provide a lot of clarity on exactly what is going on, I believe it to be the same issue you're facing.

In summary: At a minimum, call super for each delegate implementation of the UIScrollViewDelegate, or if you don't need any custom scroll behavior then remove the delegate altogether.

1
  • Can you elaborate more about calling super? I did not subclass UIWebView nor UIScrollView in this implementation. This Piece of code is in my View Controller subclass, by calling super here I am calling UIViewController which is not a UIScrollViewdelegate. By calling the superView of the scrollView, I am calling the contentView of my View Controller, which is just a UIView
    – Ben Ong
    Commented Dec 27, 2016 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.