滚动视图不能同时缩放和水平滚动吗?

时间:2019-01-24 09:26:10

标签: ios swift uiscrollview zooming

我是iOS编程的新手。我想进行缩放和水平滚动。页面加载完成后,我无法向上滚动以进行缩放。当我捏时,我就可以滚动。这是什么原因?

这是我的代码。

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
}

1 个答案:

答案 0 :(得分:0)

三个步骤:

  1. 首先:确保滚动视图的最大缩放比例更大 而不是默认值1.0。您可以在Interface Builder中更改此设置 所需,或在代码中使用maximumZoomScale属性。
  2. 使视图控制器成为滚动视图的delegate
  3. 使您的视图控制器符合UIScrollViewDelegate协议,然后添加viewForZooming(in:)

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