UIscrollView 快速分页和缩放图像

Posted

技术标签:

【中文标题】UIscrollView 快速分页和缩放图像【英文标题】:UIscrollView Paging and Zooming images in swift 【发布时间】:2016-06-22 13:07:08 【问题描述】:

有一个图像数组(从服务器下载),我想在滚动视图中显示并启用分页和缩放,但我没有找到任何解决方案。我搜索了很多,但我没有得到任何解决方案。

任何帮助都非常感激。 谢谢!

在主控制器中

 var mainScrollView: UIScrollView!


override func viewDidLoad() 
    super.viewDidLoad()
    var x:CGFloat = 0
    mainScrollView = UIScrollView(frame: view.bounds)
    mainScrollView.pagingEnabled = true
    let images=["image.png","photo.jpg","3.png"]
    for var i = 0; i < images.count; i++
    

      let  imageView = UIImageView(image: UIImage(named: images[i]))

        let scrollView = SlideShow(image: imageView, scrollFrame: CGRectMake(x, view.bounds.origin.y, view.bounds.width, view.bounds.height))

        x = x + UIScreen.mainScreen().bounds.size.width
       mainScrollView.addSubview(scrollView)
    
    mainScrollView.contentSize=CGSizeMake(x, self.mainScrollView.frame.size.height);
    mainScrollView.contentOffset=CGPointMake(0, 0);
    view.addSubview(mainScrollView)
    // Do any additional setup after loading the view.



在子类中

public class SlideShow: UIScrollView, UIScrollViewDelegate 

public var imageView = UIImageView()


// MARK: - Life cycle

init(image: UIImageView, scrollFrame: CGRect) 

    super.init(frame: scrollFrame)

    //image.setToImageView(imageView)
    imageView = image
    imageView.clipsToBounds = true
    imageView.userInteractionEnabled = true

    // UIScrollView(frame: bounds)
    delegate = self
    showsVerticalScrollIndicator = false
    showsHorizontalScrollIndicator = false
    backgroundColor = UIColor.blackColor()
   // contentSize = imageView.bounds.size
   // autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    contentOffset = CGPoint(x: 1000, y: 450)

    addSubview(imageView)

   // setZoomScale()

    setupGestureRecognizer()




func setupGestureRecognizer() 
    let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
    doubleTap.numberOfTapsRequired = 2
    addGestureRecognizer(doubleTap)


func handleDoubleTap(recognizer: UITapGestureRecognizer) 

    if (zoomScale > minimumZoomScale) 
        setZoomScale(minimumZoomScale, animated: true)
     else 
        setZoomScale(maximumZoomScale, animated: true)
    



required public init?(coder aDecoder: NSCoder) 
    fatalError("init(coder:) has not been implemented")

override public func layoutSubviews() 
    setZoomScale()
    //setZoomScale(minimumZoomScale, animated: false)



public func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? 

    return imageView




func setZoomScale() 
    let imageViewSize = imageView.bounds.size
    let scrollViewSize = bounds.size
    let widthScale = scrollViewSize.width / imageViewSize.width
    let heightScale = scrollViewSize.height / imageViewSize.height

    minimumZoomScale = min(widthScale, heightScale)
    zoomScale = 1.0


public func scrollViewDidZoom(scrollView: UIScrollView) 
    let imageViewSize = imageView.frame.size
    let scrollViewSize = bounds.size

    let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
    let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

    scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
 

【问题讨论】:

请提供您的代码... 这对您没有帮助吗? raywenderlich.com/122139/uiscrollview-tutorial 没有。我正在更新问题,请看一下。 【参考方案1】:

解决方案 1:- Agrume Library with load photo url. 您可以通过多种方式使用图像查看器(包含的示例项目将全部展示)。

这是最好的库。

解决方案 2:- This library provide multiple images and zoom also.

【讨论】:

我试试这个,缩放后滚动视图中有很多额外的空间。 是否同时包含分页和缩放功能。

以上是关于UIscrollView 快速分页和缩放图像的主要内容,如果未能解决你的问题,请参考以下文章

在 UIScrollView 中缩放和滚动大图像

启用分页的 UISCrollview

iOS 多图像分页和缩放问题

UIImage 或 UIImageView 带有分页和点击缩放

如何在启用分页的 UIScrollView 中延迟加载 100 多个页面?

iphone:UIScrollView 分页启用,无缩放和无预览