Search code examples
iosswiftuiscrollviewuiimageviewuiscrollviewdelegate

Scroll UIScrollView faster when scrolling it through another view


I have a UIScrollView with large UIImageView with image of very high resolution (about 10,000 x 10,000). In this UIImageView both zooming and scrolling is enabled. I also have a smaller UIImageView with the same image with much smaller resolution (about 100 x 100). I'm showing the visible portion of larger UIImageView on the smaller UIImageView. And the user can navigate to other places on larger UIImageView by panning on smaller UIImageView. The following images show what I'm trying to explain. My issue is the while panning on the smaller UIImageView the scrolling in larger UIScrollView really slow. enter image description here

// function that handles the pan on green view
func handlePanNavigation(gestureRecognizer: UIPanGestureRecognizer) {

    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
        let translation = gestureRecognizer.translation(in: navigationPanel)
        guard let gv = gestureRecognizer.view else { return }
        let point = CGPoint(x: gv.center.x + translation.x, y: gv.center.y + translation.y)
        gestureRecognizer.view?.center = point
        gestureRecognizer.setTranslation(.zero, in: navigationPanelView)

        let transform = CGAffineTransform(scaleX: orgSize.width*tiledScrollView.zoomScale/navSize.width, y: orgSize.height*tiledScrollView.zoomScale/navSize.height)
        let offset = navigationPanelView.frame.origin.applying(transform)
        tiledScrollView.setContentOffset(offset, animated: true)
    }
}

Solution

  • You should not animate the change of the content offset while applying a transformation of a user input real-time, since that can easily slow down the feedback.

    Change

    tiledScrollView.setContentOffset(offset, animated: true)
    

    to

    tiledScrollView.setContentOffset(offset, animated: false)