KSImageCarousel
KSImageCarousel 是一个轻量级图像轮播控件,可以轻松适应不同类型的图像源。KSImageCarousel 支持有限和无限滚动模式。只需使用您想要的 UIImages 或图像 URL 创建 KSImageCarousel 实例,它将为您完成所有工作。
需求
- Xcode 10 或更高版本
- iOS 10.3 或更高版本
- Swift 4.2 或更高版本
功能
- 无限滚动模式
- 有限滚动模式
- 自动滚动
- 支持
UIImage
和URL
作为图像源 - 易于适应其他图像源
示例
克隆或下载源代码,并使用 KSImageCarouselExample.xcworkspace
启动项目。示例包括使用无限和有限滚动模式的示例代码。
安装
CocoaPods
pod 'KSImageCarousel'
Carthage
- 创建和更新 Cartfile
github "LeeKahSeng/KSImageCarousel"
- 使用终端构建框架
carthage update
- 使用 Carthage 构建框架完成后,打开 XCode 并在项目导航器中选择您的项目。
- 在
构建阶段
选项卡中,将KSImageCarousel.framework
添加到链接二进制与库
。 - 在
常规
选项卡中,将KSImageCarousel.framework
添加到嵌入式二进制文件
。
手动
- 下载项目。
- 将
\KSImageCarousel\KSImageCarousel
中的Source
文件夹拖到您的 Xcode 项目中。 - 将
import UIKit
添加到所有造成编译错误的源代码中。 - 构建 & 运行。
如何使用
KSImageCarousel 使用协调器模式开发。因此,要开始使用 KSImageCarousel,创建一个 KSICInfiniteCoordinator
或 KSICFiniteCoordinator
,并使用它来在一个希望包含的容器视图中显示轮播图。
如果使用 CocoaPods 或 Carthage,请确保导入 KSImageCarousel。
import KSImageCarousel
要使用有限滚动模式的 KSImageCarousel。
// Create container view
let carouselContainer = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 150))
view.addSubview(carouselContainer)
// Create array of image source
let model = [
URL(string: "https://via.placeholder.com/375x281/403ADA/FFFFFF?text=Image-0")!,
URL(string: "https://via.placeholder.com/375x281/5D0F25/FFFFFF?text=Image-1")!,
URL(string: "https://via.placeholder.com/375x281/83B002/FFFFFF?text=Image-2")!,
URL(string: "https://via.placeholder.com/375x281/1B485D/FFFFFF?text=Image-3")!,
URL(string: "https://via.placeholder.com/375x281/E6581C/FFFFFF?text=Image-4")!,
]
// Use coordinator to show the carousel
if let coordinator = try? KSICFiniteCoordinator(with: model, placeholderImage: nil, initialPage: 0) {
coordinator.activityIndicatorStyle = .white
coordinator.showCarousel(inside: carouselContainer, of: self)
}
要使用无限滚动模式的 KSImageCarousel
// Create container view
let carouselContainer = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 150))
view.addSubview(carouselContainer)
// Create array of image source
let model = [
URL(string: "https://via.placeholder.com/375x281/403ADA/FFFFFF?text=Image-0")!,
URL(string: "https://via.placeholder.com/375x281/5D0F25/FFFFFF?text=Image-1")!,
URL(string: "https://via.placeholder.com/375x281/83B002/FFFFFF?text=Image-2")!,
URL(string: "https://via.placeholder.com/375x281/1B485D/FFFFFF?text=Image-3")!,
URL(string: "https://via.placeholder.com/375x281/E6581C/FFFFFF?text=Image-4")!,
]
// Use coordinator to show the carousel
if let coordinator = try? KSICInfiniteCoordinator(with: model, placeholderImage: nil, initialPage: 0) {
coordinator.activityIndicatorStyle = .white
coordinator.showCarousel(inside: carouselContainer, of: self)
}
为 KSICInfiniteCoordinator 启用自动滚动
coordinator.startAutoScroll(withDirection: .left, interval: 1)
在图片下载时隐藏/显示活动指示器视图
coordinator.shouldShowActivityIndicator = true
更改活动指示器视图样式
coordinator.activityIndicatorStyle = .white
请注意,在调用 showCarousel(inside: UIView, of: UIViewController)
之前,需要设置 shouldShowActivityIndicator
和 activityIndicatorStyle
。
要检测轮播图上的轻击事件,只需遵守 KSICCoordinatorDelegate
并实现 carouselDidTappedImage(at index: Int, coordinator: KSICCoordinator)
。
您可以随意克隆或下载源代码,查看 KSImageCarouselExample 项目,此项目重点介绍了如何正确使用 KSImageCarousel。
适应使用其他类型的图像源
目前 KSImageCarousel 支持同时使用 UIImage
和 URL
作为图像源。然而,可能存在需要 KSImageCarousel 支持其他图像源的情况,例如 Base64、字节数组等,或者您希望使用项目自己的图像下载器而不是 KSImageCarousel 内置的下载器。您需要做的只是创建一个自定义图像提供程序类,并遵守 KSImageCarouselDisplayable
协议。
支持 Base64 图像源的示例类
class Base64ImageProvider {
let base64: String
init(_ base64: String) {
self.base64 = base64
}
func covertToImage(completion: @escaping (UIImage?) -> Void) {
/*** Code to covert Base64 to UIImage here ***/
let convsionResult = UIImage()
/*********************************************/
// Call completion handler with the conversion result
completion(convsionResult)
}
}
extension Base64ImageProvider: KSImageCarouselDisplayable {
func createCarouselImage(completion: @escaping (UIImage?, KSImageCarouselDisplayable) -> Void) {
covertToImage { (image) in
completion(image, self)
}
}
func isEqual(to displayable: KSImageCarouselDisplayable) -> Bool {
if let provider = displayable as? Base64ImageProvider {
return base64 == provider.base64
} else {
return false
}
}
}
使用其他图像下载器的示例类
class ImageProvider {
let url: URL
init(_ url: URL) {
self.url = url
}
func downloadImage(completion: @escaping (UIImage?) -> Void) {
/*** Code to download image using you own downloader class here ***/
let downloadResult = UIImage()
/******************************************************************/
// Call completion handler with the conversion result
completion(downloadResult)
}
}
extension ImageProvider: KSImageCarouselDisplayable {
func createCarouselImage(completion: @escaping (UIImage?, KSImageCarouselDisplayable) -> Void) {
downloadImage { (downloadedImage) in
completion(downloadedImage, self)
}
}
func isEqual(to displayable: KSImageCarouselDisplayable) -> Bool {
if let provider = displayable as? ImageProvider {
return url == provider.url
} else {
return false
}
}
}
许可证
此代码根据 MIT 许可证的条款和条件进行分发。