测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT 许可证 |
发布上次发布 | 2017 年 3 月 |
SwiftSwift 版本 | 3.1 |
SPM支持 SPM | ✗ |
由 ljlin1520 维护。
Swift 实现的循环轮播
1. 简单易用,支持 CocoaPods
、Carthage
;
2. 自带缓存,缓存参考猫神的 Kingfisher;
3. 支持 GIF 动图,无第三方依赖
4. 也可以自己注册 Cell,更灵活地展示自定义 View
先不聊了,上图
//step 1
import FCCarouselView
//step 2
//MARK: getter 懒加载
fileprivate lazy var carouselView:CarouselView = {
let carouselView = CarouselView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: 200))
carouselView.delegate = self
//1
var carouselData = CarouselData()
carouselData.image = UIImage(named: "1")
carouselData.detail = "I created a swift class with string optionals (String?) and instantiated the class in a different swift file and got a compile error."
carouselView.dataSource.append(carouselData)
//2
carouselData = CarouselData()
carouselData.imageURL = URL(string: "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSMFynE3clrgzCU2ZDw9SDn5gM2JuwEsCE37Qf4S6uBlJljejEYWg")
carouselData.detail = "When I instantiate the class"
carouselView.dataSource.append(carouselData)
//3
carouselData = CarouselData()
carouselData.image = UIImage(named: "3")
carouselView.dataSource.append(carouselData)
//4
carouselData = CarouselData()
carouselData.imageURL = URL(string: "https://g.twimg.com/blog/blog/image/Cat-party.gif")
carouselData.detail = "If the var item = ShoppingListItem() is done in the appDelegate.swift, from the function application:didFinishLaunchingWithOptions we get the error"
carouselView.dataSource.append(carouselData)
//5
carouselData = CarouselData()
carouselData.imageURL = URL(string: "https://www.baidu.com/123")
carouselData.detail = "<class> cannot be initialised because it has no accessible initializers"
carouselView.dataSource.append(carouselData)
return carouselView
}()
//step 3
override func viewDidLoad() {
view.addSubview(carouselView)
}
public enum AutoScrollOption {
case enable(Bool) //是否开启自动轮播,默认开启
case timeInterval(Foundation.TimeInterval) //轮播频率
}
//such as
sbCarouselView.autoScrollOptions = [.enable(false)]
public enum PageControlOption {
case hidden(Bool)
case indicatorTintColor(UIColor)
case currentIndicatorTintColor(UIColor)
}
//such as
sbCarouselView.pageControlOptions = [.indicatorTintColor(UIColor.greenColor())
, .currentIndicatorTintColor(UIColor.grayColor())]
@objc public protocol CarouselViewDelegate: class {
//自定义Cell时才使用到
@objc optional func carouselView(_ view:CarouselView, cellAtIndexPath indexPath:IndexPath, pageIndex: Int) -> UICollectionViewCell
//点击事件
@objc optional func carouselView(_ view:CarouselView, didSelectItemAtIndex index:NSInteger)
}
//step 1 registerClass
carouselView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(CustomCollectionViewCell.self))
//step 2
func carouselView(_ view: CarouselView, cellAtIndexPath indexPath: IndexPath, pageIndex: Int) -> UICollectionViewCell {
let customCell = carouselView.dequeueReusableCellWithReuseIdentifier(NSStringFromClass(CustomCollectionViewCell.self), forIndex: pageIndex) as! CustomCollectionViewCell
if let detail = carouselView.dataSource[pageIndex] as? String {
customCell.detailLabel.text = detail
}
return customCell
}
更多信息请查看 Demo
有任何问题可以提出 issues 或联系我
微博 : @飛呈jerry
博客: https://www.fcgeek.com
FCCarouselView
在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。