ImageScrollView

这是一个控件,可以帮助您轻松地显示可缩放和可滚动的图像。

关于

当您开发一个具有照片查看功能的应用程序时,照片查看器通常需要具有缩放和滚动功能,以便您的用户可以查看更多照片细节。 此控件将帮助您轻松地显示具有缩放和滚动功能的图像。

Demo

兼容性

用法

Cocoapod

将以下行添加到您的 Podfile 中

pod 'ImageScrollView'

然后在终端中运行以下命令进行安装

pod install

注意:如果上面的 pod 不起作用,请尝试在 Podfile 中使用以下 pod 定义

pod 'ImageScrollView', :git => 'https://github.com/huynguyencong/ImageScrollView.git'

Swift Package Manager

在 Xcode 中,选择菜单 File -> Swift Packages -> Add Package Dependency。 选择一个目标,然后将此链接添加到输入字段:https://github.com/huynguyencong/ImageScrollView.git

手动安装

有时只想手动安装,只需将 ImageSrollView.swift 文件添加到项目中的 Sources 文件夹即可。

易于使用

将一个 UIScrollView 拖到您的 storyboard 中,在 Identity Inspector 中将 Class 和 Module 更改为 ImageScrollView。 此外,在您的源文件中创建一个 IBOutlet。

How to config ImageScrollView in storyboard

import ImageScrollView
@IBOutlet weak var imageScrollView: ImageScrollView!
// Important: This setup method should be called once, usually in your viewDidLoad() method
imageScrollView.setup()

let myImage = UIImage(named: "my_image_name")
imageScrollView.display(image: myImage)

就这样。 现在尝试缩放和滚动以查看结果。

您可以设置 delegate 来捕获事件。 此 delegate 继承自 UIScrollViewDelegate

imageScrollView.imageScrollViewDelegate = self
extension ViewController: ImageScrollViewDelegate {
    func imageScrollViewDidChangeOrientation(imageScrollView: ImageScrollView) {
        print("Did change orientation")
    }
    
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        print("scrollViewDidEndZooming at scale \(scale)")
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("scrollViewDidScroll at offset \(scrollView.contentOffset)")
    }
}

注意

view.layoutIfNeeded()

关于此源代码

此开源基于 Apple 网站上提供的 PhotoScroller 演示。 原始源代码是用 Objective C 编写的。 此开源使用 Swift 重写了它,并添加了一些新功能

许可

ImageScrollView 在 MIT 许可下发布。 有关详细信息,请参阅 LICENSE。 版权所有 © Nguyen Cong Huy