Search code examples
iosswiftuiscrollviewzooming

scroll view doesnt zooming and horizontal scrolling together?


I'm newbie at iOS programming. I want to do zooming and horizontal scrolling. When the page did load, I can't scrolling up to pinch. when I pinch, then I'm able to scroll. What's the reason of this?

Here's my code.

override func viewDidLoad() {
    super.viewDidLoad()
    scrollview.delegate = self
    //mark:- for scrolling
    let scrollViewFrame = scrollview.frame
    let scaleWidth = scrollViewFrame.size.width / scrollview.contentSize.width
    let scaleHeight = scrollViewFrame.size.height / scrollview.contentSize.height
    let minScale = min(scaleWidth, scaleHeight);
    scrollview.minimumZoomScale = minScale;

    scrollview.maximumZoomScale = 1.0
    scrollview.zoomScale = 0.8


}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    statsView.drawStatsView(color: #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1), average: 100, min: 90, max: 110, name: "H")
    statsView.drawStatsView(color: #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1), average: 120, min: 102, max: 122, name: "A")
    statsView.drawStatsView(color: #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1), average: 145, min: 130, max: 150, name: "Y")
}
override func viewDidLayoutSubviews() {
   // bunu bi düşün containera statsView.Container.addConstraint()
    scrollview.contentSize = CGSize(width: 720, height: scrollview.frame.height)
    scrollview.alwaysBounceHorizontal = false
    scrollview.bounces = false
    scrollview.clipsToBounds = true

}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    print("we re zooming yey")
    return statsView
}

Solution

  • Three steps:

    1. First: Make sure your scroll view has a maximum zoom scale larger than the default of 1.0. You can change this in Interface Builder if you want, or use the maximumZoomScale property in code.
    2. Make your view controller the delegate of your scroll view.
    3. Make your view controller conform to the UIScrollViewDelegate protocol, then add the viewForZooming(in:)

      func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? { 
      return imageView
      }