ScalingCarousel
ScalingCarousel 提供了一个简单的轮播式集合视图。它负责单元格的呈现,并在集合视图滚动时缩放每个单元格。
它被用于 Bikey 来呈现自行车站信息,如下所示;
使用方法
如以下所述,ScalingCarousel 可以通过 storyboards/xib 和代码添加。
Storyboard
-
将 UICollectionView 添加到您的视图中,并将其类型更改为 ScalingCarouselView
-
在属性检查器中,设置所需的轮播内边距
-
将您的 UIViewController 设置为集合视图的数据源,并在您的视图中实现标准的 UICollectionViewDatasource 方法
-
将您的 UIViewController 设置为集合视图的委托,并实现 UIScrollViewDelegate 方法 scrollViewDidScroll(:)。在这个方法中,调用 ScalingCarouselView 的 didScroll() 方法
-
创建一个继承自 ScalingCarouselCell 的自定义 UICollectionViewCell,并在 storyboards 中将单元格类型设置为您的自定义单元格类型
-
将视图添加到单元格的内容视图中,并通过连接检查器(在 Interface builder 中)连接到单元格的主要视图 IBOutlet。这个属性在 ScalingCarouselCell 中声明。您应该在视图中添加任何单元格内容。
-
注意:为确保ScalingCarouselCell的缩放正确,您需要在将数据配置到单元格中(例如在
cellForItem(at:)
)后调用以下代码:
cell.setNeedsLayout()
cell.layoutIfNeeded()
- 注意:为确保正确显示ScalingCarousel,您需要在ViewController的
viewWillTransition(to size:, with coordinator:)
方法中调用以下代码
super.viewWillTransition(to: size, with: coordinator)
scalingCarousel.deviceRotated()
代码
- 创建一个继承自ScalingCarouselCell的自定义UICollectionViewCell。初始化mainView属性,该属性在ScalingCarouselCell中声明;
override init(frame: CGRect) {
super.init(frame: frame)
// Initialize the mainView property and add it to the cell's contentView
mainView = UIView(frame: contentView.bounds)
contentView.addSubview(mainView)
mainView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mainView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
mainView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
mainView.topAnchor.constraint(equalTo: contentView.topAnchor),
mainView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
- 在您的视图中创建并添加一个ScalingCarouselView,并在您的视图中实现标准UICollectionViewDatasource方法;
// Create our carousel
let scalingCarousel = ScalingCarouselView(withFrame: frame, andInset: 20)
scalingCarousel.dataSource = self
scalingCarousel.delegate = self
scalingCarousel.translatesAutoresizingMaskIntoConstraints = false
// Register our custom cell for dequeueing
scalingCarousel.register(Cell.self, forCellWithReuseIdentifier: "cell")
// Add our carousel as a subview
view.addSubview(scalingCarousel)
// Add Constraints
scalingCarousel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1).isActive = true
scalingCarousel.heightAnchor.constraint(equalToConstant: 300).isActive = true
scalingCarousel.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scalingCarousel.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
-
将您的 UIViewController 设置为集合视图的委托,并实现 UIScrollViewDelegate 方法 scrollViewDidScroll(:)。在这个方法中,调用 ScalingCarouselView 的 didScroll() 方法
-
注意:为确保ScalingCarouselCell的缩放正确,您需要在将数据配置到单元格中(例如在
cellForItem(at:)
)后调用以下代码:
cell.setNeedsLayout()
cell.layoutIfNeeded()
- 注意:为确保正确显示ScalingCarousel,您需要在ViewController的
viewWillTransition(to size:, with coordinator:)
方法中调用以下代码,如果您是在viewDidLoad
中以代码创建的ScalingCarousel,则非常重要地验证它在调用viewWillTransition
方法时存在,否则如果在横屏模式下加载viewController,我们将遇到崩溃;
super.viewWillTransition(to: size, with: coordinator)
if scalingCarousel != nil {
scalingCarousel.deviceRotated()
}
示例
要运行示例项目,请首先克隆repo,然后首先从Example目录运行pod install
。
需求
iOS 10
安装
ScalingCarousel可以通过CocoaPods、Carthage和Swift 包管理器获得。
要通过CocoaPods安装,请将以下行添加到您的Podfile中:
pod "ScalingCarousel"
要通过Carthage安装,请将以下行添加到您的Podfile中:
github "superpeteblaze/ScalingCarousel"
要通过Swift包管理器安装:
注意:以下说明是为使用不带Xcode UI的SwiftPM。这可以通过前往项目设置 -> Swift Packages并从那里添加ScalingCarousel来实现,这是最简单的。
要使用不带Xcode集成的Apple的Swift包管理器进行集成,请将以下内容添加到您的Package.swift
中作为依赖项:
.package(url: "https://github.com/superpeteblaze/ScalingCarousel.git", .upToNextMajor(from: "3.2.0"))
作者
Pete Smith,[email protected]
许可
ScalingCarousel在MIT许可下可用。更多信息请参阅LICENSE文件。