Auk,iOS 上的图像轮播/Swift
这是一个 iOS 库,显示带有页面指示器的图像轮播图。用户可以滚动查看本地和远程图像,或自动滚动它们。
scrollView.auk.show(url: "https://bit.ly/auk_image")
scrollView.auk.show(url: "https://bit.ly/moa_image")
if let image = UIImage(named: "local_bird.jpg") {
scrollView.auk.show(image: image)
}
- 使用自动布局,并在屏幕方向更改期间支持动画过渡。
- 允许指定远程源占位符和错误图像。
- 包含远程图像的缓存和日志记录。
- 支持从右到左的语言。
- 允许指定图像的无障碍标签。
- 包括在单元测试中模拟和验证图像下载的能力。
约1900年由约翰·杰拉德·库伊勒曼斯绘制的猛禽图案。来源:Wikipedia Commons。
设置
您可以将 Auk 添加到您的 Xcode 项目的多种方式。
添加源(iOS 7+)
简单地将两个文件添加到您的项目中:
- Moa 图像下载器 MoaDistrib.swift。
- Auk 图像轮播 AukDistrib.swift。
使用 Carthage (iOS 8+) 设置
- 将
github "evgenyneu/Auk" ~> 11.0
添加到您的 Cartfile。 - 运行
carthage update
。 - 将
moa
和Auk
框架添加到您的项目中。
使用 CocoaPods (iOS 8+) 设置
如果您正在使用 CocoaPods,请将以下文本添加到您的 Podfile 中,并运行 pod install
。
use_frameworks!
target 'Your target name'
pod 'moa', '~> 12.0'
pod 'Auk', '~> 11.0'
使用 Swift 包管理器设置
- 在 Xcode 11+ 中选择 文件 > 包 > 添加包依赖...。
- 输入 moa 项目的 URL(图片下载依赖项):https://github.com/evgenyneu/moa.git
- 为 Auk URL 重复此操作:https://github.com/evgenyneu/Auk.git
旧版 Swift 版本
如果您使用的是旧版 Swift,请设置该库的先前版本。
使用说明
- 将
import Auk
添加到源代码(除非您使用了文件设置方法)。 - 在 Storyboard 中添加一个滚动视图,并在您的视图中创建 Outlet 属性
scrollView
。 - 取消选中视图控制器的 属性检查器 中的 调整滚动视图内边距 复选框。
Auk 通过创建 auk
属性扩展了 UIScrollView
类。
// Show remote images
scrollView.auk.show(url: "https://bit.ly/auk_image")
scrollView.auk.show(url: "https://bit.ly/moa_image")
// Show local image
if let image = UIImage(named: "bird.jpg") {
scrollView.auk.show(image: image)
}
// Return the number of pages in the scroll view
scrollView.auk.numberOfPages
// Get the index of the current page or nil if there are no pages
scrollView.auk.currentPageIndex
// Return currently displayed images
scrollView.auk.images
从代码滚动
// Scroll to page
scrollView.auk.scrollToPage(atIndex: 2, animated: true)
// Scroll to the next page
scrollView.auk.scrollToNextPage()
// Scroll to the previous page
scrollView.auk.scrollToPreviousPage()
自动滚动
// Scroll images automatically with the interval of 3 seconds
scrollView.auk.startAutoScroll(delaySeconds: 3)
// Stop auto-scrolling of the images
scrollView.auk.stopAutoScroll()
注意,当用户开始手动滚动时,自动滚动将停止。
无障碍
在调用show
方法时,可以传入图像描述。该描述将在辅助功能模式下读出当前屏幕上的图像。
// Supply accessibility label for the image
scrollView.auk.show(url: "https://bit.ly/auk_image", accessibilityLabel: "Picture of a great auk.")
移除页面
// Remove a page at given index
scrollView.auk.removePage(atIndex: 0, animated: true, completion: {})
// Remove the currently shown page
scrollView.auk.removeCurrentPage(animated: true, completion: {})
// Remove all pages
scrollView.auk.removeAll()
更新页面
可以通过调用updateAt
方法并传入页面索引来更改现有图像。
// Replace the image on a given page with a remote image.
// The current image is replaced after the new image has finished downloading.
scrollView.auk.updatePage(atIndex: 0, url: "https://bit.ly/moa_image")
// Replace the image on a given page with a local image.
if let image = UIImage(named: "bird.jpg") {
scrollView.auk.updatePage(atIndex: 1, image: image)
}
从非安全的HTTP主机加载图像
如果您的远程图像URL不是https,您需要将异常添加到Info.plist
文件中。这将允许App Transport Security从非安全的HTTP主机加载图像。添加异常。
配置
在使用图片之前,使用auk.settings
属性来配置滚动视图的行为和外观。请参阅配置手册以获取完整的配置选项列表。
// Make the images fill entire page
scrollView.auk.settings.contentMode = .scaleAspectFill
// Set background color of page indicator
scrollView.auk.settings.pageControl.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
// Show placeholder image while remote image is being downloaded.
scrollView.auk.settings.placeholderImage = UIImage(named: "placeholder.jpg")
// Show an image AFTER specifying the settings
scrollView.auk.show(url: "https://bit.ly/auk_image")
预加载远程图片
默认情况下,远程图片在用户看到它们时才被加载。可以通过设置preloadRemoteImagesAround
属性来让库预先加载远程图片。
// Set the property before showing remote images
scrollView.auk.settings.preloadRemoteImagesAround = 1
// Add remote images. The first two images will start loading simultaneously.
scrollView.auk.show(url: "https://bit.ly/auk_image")
scrollView.auk.show(url: "https://bit.ly/moa_image")
// The third image will start loading when the user scrolls to the second page.
scrollView.auk.show(url: "https://bit.ly/auks_at_home")
preloadRemoteImagesAround
属性定义了围绕当前页面预加载的远程图片的数量。例如,如果preloadRemoteImagesAround
= 2,并且我们正在查看第一页,则它将对第二页和第三页的图片进行预加载。如果我们正在查看第五页,那么它将对第三页、第四页、第六页和第七页的图片进行预加载(除非它们已经加载)。默认值是0。
请注意,图片同时加载,因此,如果使用较大的preloadRemoteImagesAround
值,由于有限的带宽将被许多图片下载共享,可能会延迟最早的图片。
大小变化动画
如果需要在与设备方向变化时对滚动视图进行动画处理,请阅读大小动画手册。
图片缓存
Auk使用moa图片下载器来获取远程图片。可以通过更改Moa.settings.cache.requestCachePolicy
属性来配置其缓存设置。如果您使用了Carthage或CocoaPods设置方法,请在源代码中添加import moa
。
import moa // for Carthage and CocoaPods
// ...
// By default images are cached according to their response HTTP headers.
Moa.settings.cache.requestCachePolicy = .useProtocolCachePolicy
// Use local cache regardless of response HTTP headers.
Moa.settings.cache.requestCachePolicy = .returnCacheDataElseLoad
注意:moa图片下载器提供了其他功能,包括请求日志和HTTP设置。
日志记录
如果您想了解远程图像何时正在加载,可以将网络活动记录到控制台,如下例所示。有关更多信息,请参阅Moa日志手册。
import moa // for Carthage and CocoaPods
// ...
// Log to console
Moa.logger = MoaConsoleLogger
// Show an existing image
scrollView.auk.show(url: "https://bit.ly/auk_image")
// Attempt to show a missing image
scrollView.auk.show(url: "https://bit.ly/missing_auk.jpg")
远程图像单元测试
可以在您的单元测试中模拟和验证远程图像下载。有关更多信息,请参阅Moa单元测试手册。如果您使用Carthage或CocoaPods设置方法,请将import moa
添加到源代码中。
// Autorespond with the given image
MoaSimulator.autorespondWithImage("www.site.com", image: UIImage(named: "35px.jpg")!)
响应图像点击
要添加图像点击处理器到滚动视图,请执行以下步骤。
- 在Storyboard中,将一个点击手势识别器拖放到您的滚动视图中。
- 显示辅助编辑器并显示您的视图控制器代码。
- 从Storyboard中的点击手势识别器进行控制拖动到您的视图控制器代码中。
- 将出现一个对话框,将连接更改为action并输入方法名。
- 当滚动视图被点击时将调用此方法。使用您的滚动视图的
auk.currentPageIndex
属性来获取当前页面的索引。
检测页面滚动
您可以使用UIScrollViewDelegate
在滚动视图被滚动时运行一些代码。有关详细信息,请参阅检测页面滚动手册。
在 Objective-C 中使用 Auk
本手册描述了如何在 Objective-C 应用中使用 Auk。
常见问题
页面控制不可见
确保在调用 show
方法之前将 scrollView
添加到视图树中。否则页面控制将不会创建。快速检查
print(scrollView.superview) // should not be `nil`
scrollView.auk.show(url: "https://bit.ly/auk_image")
当调用 show
方法时,页面控制会被添加到滚动视图的父视图中。这就是为什么当滚动视图没有父视图时页面控制不会创建的原因。
如果 scrollView.superview
为 nil
,那么你可能需要将显示图片的代码移动到 viewDidAppear
方法中。
远程图像未加载
可以开启 日志记录器 以查看 Xcode 控制台中的网络活动,并找到图像下载的问题。
页面控制未更改 / 第二个远程图像未显示
如果您正在分配滚动视图的委托,请确保在显示图像之前执行。
// Assign the delegate BEFORE showing the images
scrollView.delegate = self
scrollView.auk.show(url: "https://bit.ly/auk_image")
scrollView.auk.show(url: "https://bit.ly/moa_image")
示例应用
项目包含一个 iOS 示例应用。
其他解决方案
以下是iOS的其他图片轮播库列表。
- kimar/KIImagePager
- kirualex/KASlideShow
- nicklockwood/iCarousel
- nicklockwood/SwipeView
- paritsohraval100/PJR-ScrollView-Slider
- zvonicek/ImageSlideshow
👍
谢谢
图片来源
- 约翰·詹姆斯·奥杜邦于1827-1838年绘制的北极燕鸥画作。来源:维基共享资源。
- 1900年左右,约翰·杰拉德·克吕勒曼斯描绘的带有幼鸟的北极燕鸥。来源:维基共享资源。
- 1902-1903年间《科学美国人杂志》第62卷中的北极燕鸥画作。来源:维基共享资源。
- 来自1919年亚瑟·克利夫兰·本特著作《美国鸟类》的北极燕鸥蛋。来源:维基共享资源。
- 奥洛夫·沃穆乌斯于1655年绘制的北极燕鸥具象画作。来源:维基共享资源。
- 约翰·杰拉德·克吕勒曼斯绘制的《北极燕鸥在家的油画》。来源:维基共享资源。
- 约翰·古尔德绘制的杓鹬:欧洲之鸟,第5卷第55页,19世纪。来源:维基共享资源。
- 约翰·杰拉德·克吕勒曼斯绘制的夏季和冬季羽毛的北极燕鸥,早于1912年。来源:维基共享资源。
许可
Auk 发布在 MIT 许可证 下。
欢迎反馈
如果您发现任何问题、遇到难题或者只是想聊天,请随意创建一个反馈issue。我将很乐意帮助您。
•ᴥ•
本代码献给