简体   繁体   English

具有多个图像和缩放的Scrollview

[英]Scrollview with multiple images and zoom

I have a problem zooming images in my scrollview. 我在滚动视图中缩放图像时遇到问题。 When there is only one image then it is working fine but when there are more images then the following images are on top of the first. 当只有一个图像时,它工作正常,但是当有更多图像时,下面的图像位于第一个图像的顶部。 They keep there position but first is larger. 他们保持位置,但首先是更大。

Do I have to update the offset of the following images or is there another approach? 我是否必须更新以下图像的偏移量,还是有其他方法? When I zoom an 我缩放时

@IBOutlet weak var mainScrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad() {
    ...
    self.mainScrollView.delegate = self
    self.mainScrollView.minimumZoomScale = 1.0
    self.mainScrollView.maximumZoomScale = 3.0
    self.mainScrollView.isPagingEnabled = true
    self.mainScrollView.isScrollEnabled = true
    self.loadImages() 
    ...
}

@objc fund loadData() {
    ...
  scrollViewWidth = self.mainScrollView.frame.width
  scrollViewHeight = self.mainScrollView.frame.height
    for dImage in self.imgData! {
        let imageView = UIImageView(frame: CGRect(x:offset, y:0,width:self.scrollViewWidth, height:self.scrollViewHeight))
        imageView.image = UIImage(data: dImage)
        imageView.contentMode = .scaleAspectFit
        self.imgViewArray.append(imageView)
        self.mainScrollView.addSubview(imageView)

        offset += self.scrollViewWidth
        self.mainScrollView.contentSize = CGSize(width: offset, height: self.scrollViewHeight)
        }
    }
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {  
    let page = scrollView.contentOffset.x / 
    scrollView.frame.size.width;
    self.imageView = imgViewArray[Int(page)]
    return self.imageView
}

Here I zoomed the another image. 在这里,我放大了另一个图像。 Also wrong position. 也是错误的立场。

Would be nice if someone can point me into the right direction. 如果有人能指出我正确的方向会很好。 Thank You! 谢谢!

Add auto layout constraints to position the image views (spacing one below the other) within your scroll view's content view. 添加自动布局约束以在滚动视图的内容视图中定位图像视图(间隔一个在另一个之下)。

UIScrollView tutorial to scroll and zoom content UIScrollView教程可滚动和缩放内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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