{"id":22491989,"url":"https://github.com/zgjff/jjcarouselview","last_synced_at":"2025-07-06T18:12:47.056Z","repository":{"id":57210110,"uuid":"479258436","full_name":"zgjff/JJCarouselView","owner":"zgjff","description":"泛型、block、无任何第三方、易于扩展的轮播图框架","archived":false,"fork":false,"pushed_at":"2023-09-18T02:25:55.000Z","size":2055,"stargazers_count":21,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-24T17:17:24.673Z","etag":null,"topics":["ios9","swift5"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zgjff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-08T05:38:56.000Z","updated_at":"2024-11-07T16:19:35.000Z","dependencies_parsed_at":"2022-08-29T11:41:31.269Z","dependency_job_id":null,"html_url":"https://github.com/zgjff/JJCarouselView","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/zgjff/JJCarouselView","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zgjff%2FJJCarouselView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zgjff%2FJJCarouselView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zgjff%2FJJCarouselView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zgjff%2FJJCarouselView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zgjff","download_url":"https://codeload.github.com/zgjff/JJCarouselView/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zgjff%2FJJCarouselView/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263949409,"owners_count":23534324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios9","swift5"],"created_at":"2024-12-06T18:12:58.739Z","updated_at":"2025-07-06T18:12:47.009Z","avatar_url":"https://github.com/zgjff.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"JJCarouselView\n=================\n\n[![GitHub](https://img.shields.io/github/license/zgjff/JJCarouselView)](https://www.apache.org/licenses/LICENSE-2.0.html)\n[![swift-5.0](https://img.shields.io/badge/swift-5.0-blue)](https://www.swift.org)\n![iOS-9.0](https://img.shields.io/badge/iOS-9.0-red)\n[![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/zgjff/JJCarouselView)](https://github.com/zgjff/JJCarouselView)\n[![SwiftPM support](https://img.shields.io/badge/SwiftPM-support-brightgreen)](https://www.swift.org/package-manager/)\n[![Cocoapods](https://img.shields.io/cocoapods/v/JJCarouselView)](https://cocoapods.org/pods/JJCarouselView)\n\n适用于Swift的简单好用、易于扩展的轮播图框架\n\n使用方法\n=================\n\n## 一、初始化\n因为本轮播图是泛型控件,所以在初始化的时候需要指定类型。\n```swift\nlet carouselView: JJCarouselView\u003cUIImageView, UIImage\u003e = JJCarouselView(frame: CGRect.zero)\n\nlet carouselView: JJCarouselView\u003cUIImageView, UIImage\u003e = JJCarouselView(frame: CGRect.zero) {\n    return UIImageView()\n}\n```\n\n## 二、使用方法\n\n### 2.1 基本设置\n轮播图方向,默认从左-\u003e右\n```swift\ncv.config.direction = .ltr\n```\n\n是否自动轮播,默认`true`自动轮播\n```swift\ncv.config.autoLoop = true\n```\n\n轮播间隔,默认5s\n```swift\ncv.config.loopTimeInterval = 5\n```\n\n轮播图内边局\n```swift\ncv.config.contentInset = .zero\n```\n\n\n### 2.2 展示数据\n\u003e 本控件没指定将对应的数据源显示到容器的方法,所以需要自己去实现。只要在初始化`JJCarouselView`之后,设置`config.display`即可。\n\n#### 2.2.1 以最基本的展示本地图片的轮播图为例:\n```swift\nlet carouselView: JJLocalImageCarouselView\ncv.config.display = { cell, image in\n    cell.clipsToBounds = true\n    cell.contentMode = .scaleAspectFill\n    cell.image = image\n}\n```\n\n#### 2.2.2 展示网络图片\n```swift\nlet carouselView: JJWebImageCarouselView\ncv.config.display = { cell, url in\n    ...\n    // 使用SDWebImage\n    cell.sd_setImage(with: url)\n    // 使用Kingfisher\n    cell.kf.setImage(with: url)\n}\n```\n\n#### 2.2.3 展示任何你想轮播的内容\n可以使用任何`UIView`的子类来展示任意对象,只需设定轮播图类型的`Object`遵守`Equatable`协议即可。\n```swift\n// 轮播Model,必须遵守Equatable协议\nstruct WebCarouselModel {\n    let title: String\n    let desc: String\n    let url: URL\n}\n\nextension WebCarouselModel: Equatable {\n    static func == (lhs: Self, rhs: Self) -\u003e Bool {\n        return (lhs.title == rhs.title) \u0026\u0026 (lhs.desc == rhs.desc)\n    }\n}\n```\n```swift\n// 轮播控件\nfinal class WebCarouselView: UIView {}\n```\n```swift\nlet cv: JJCarouselView\u003cWebCarouselView, WebCarouselModel\u003e = JJCarouselView(frame: CGRect(x: 50, y: 0, width: 200, height: 150), initialize: nil)\ncv.config.display = { cell, object in\n    cell.titleLabel.text = object.title\n    cell.descLabel.text = object.desc\n}\ncv.datas = [\n    WebCarouselModel(title: \"这是第1个自定义轮播控件\", desc: \"这是第1个自定义轮播控件\", url: URL(string: \"https://www.baidu.com\")!),\n    WebCarouselModel(title: \"这是第2个自定义轮播控件\", desc: \"这是第2个自定义轮播控件\", url: URL(string: \"https://www.zhihu.com\")!),\n    WebCarouselModel(title: \"这是第3个自定义轮播控件\", desc: \"这是第3个自定义轮播控件\", url: URL(string: \"https://cn.bing.com\")!),\n]\n```\n\n### 2.3 轮播指示器\n\n#### 2.3.1 指示器控件\n轮播图的`pageView`是可替换的,只需要替换成遵守`JJCarouselViewPageable`协议的类类即可。\n```swift\ncv.pageView = JJCarouselNumberPageView()\n```\n隐藏指示器,只需要将`pageView`设置成`JJCarouselHiddenPageView`。\n```swift\ncv.pageView = JJCarouselHiddenPageView()\n```\n当然你也可以自定义专属于你的指示器\n```swift\nclass YourOwnPageView: UIView, JJCarouselViewPageable {\n    ...\n}\n```\n```swift\ncv.pageView = YourOwnPageView()\n```\n\n#### 2.3.2 指示器控件`frame`\n默认底部居中显示指示器,当然你也可以设定任何位置。\n```swift\n// 根据数量来设定frame\ncv.config.pageViewFrame = { pageView, _, carouselViewSize, totalDataCount in\n    let pageSize = pageView.size(forNumberOfPages: totalDataCount)\n    return CGRect(x: carouselViewSize.width - pageSize.width - 12, y: carouselViewSize.height - pageSize.height - 10, width: pageSize.width, height: pageSize.height)\n}\n```\n```swift\n// 固定大小\ncv.config.pageViewFrame = { _, _, carouselViewSize, _ in\n    return CGRect(x: carouselViewSize.width - 55, y: carouselViewSize.height - 30, width: 45, height: 20)\n}\n```\n\n### 2.4 事件回调\n\n点击事件\n```swift\n// 使用block\ncv.event.onTap = { view, obj, index in\n    ...\n}\n// 使用Combine框架\ncv.event.onTapPublisher.sink { view, obj, idx in\n    ...\n}.store(in: \u0026cancellable)\n```\n准备滑动到具体的index\n```swift\n// 使用block\ncv.event.willMove = { idx in\n    ...\n}\n// 使用Combine框架\ncv.event.willMovePublisher.sink(receiveValue: { idx in\n    ...\n}).store(in: \u0026cancellable)\n```\n已经滑动到具体的index\n```swift\n// 使用block\ncv.event.didMove = { idx in\n    ...\n}\n// 使用Combine框架\ncv.event.didMovePublisher.sink(receiveValue: { idx in\n    ...\n}).store(in: \u0026cancellable)\n```\n滑动回调(当前index, 目标index, 进度)\n```swift\n// 使用block\ncv.event.onScroll = { fromIndex, toIndex, progress in \n    ...\n}\n// 使用Combine框架\ncv.event.onScrollPublisher.sink(receiveValue: { fromIndex, toIndex, progress in\n    ...\n}).store(in: \u0026cancellable)\n```\n\n\n使用需求\n=================\n* iOS 9.0+\n* Swift 5+\n\n安装\n=================\nSwift Package Manager\n* File \u003e Swift Packages \u003e Add Package Dependency\n* Add https://github.com/zgjff/JJCarouselView.git\n\nCocoapods\n```\nuse_frameworks!\npod 'JJCarouselView'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzgjff%2Fjjcarouselview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzgjff%2Fjjcarouselview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzgjff%2Fjjcarouselview/lists"}