ScalingCarousel 提供了一个简单的轮播样式集合视图。它负责单元格的呈现,并在集合视图滚动时缩放每个单元格。
它被用于 Bikey 来展示自行车站点信息,如下所示;
ScalingCarousel 可以通过 Storyboard/xib 和代码添加,如下所述。
向您的视图添加一个 UICollectionView,并将类型更改为 ScalingCarouselView
在属性检查器中,设置所需的轮播内边距
将您的 UIViewController 设置为集合视图的数据源,并在您的视图控制器中实现标准的 UICollectionViewDatasource 方法
将您的 UIViewController 设置为集合视图的委托,并实现 UIScrollViewDelegate 方法 scrollViewDidScroll(:)。在此方法中,调用 ScalingCarouselView 的 didScroll() 方法
创建一个继承自 ScalingCarouselCell 的自定义 UICollectionViewCell,并在 Storyboard 中将单元格类型设置为您的自定义单元格类型
向单元格的内容视图添加一个视图,并通过 Connections Inspector (在 Interface Builder 中) 将其连接到单元格的 mainView IBOutlet。此属性在 ScalingCarouselCell 中声明。您应该将任何单元格内容添加到此视图。
注意:为了确保 ScalingCarouselCell 的正确缩放,您需要在配置完单元格数据后调用以下代码 (例如在 cellForItem(at:)
中)
cell.setNeedsLayout()
cell.layoutIfNeeded()
viewWillTransition(to size:, with coordinator:)
方法中调用以下代码super.viewWillTransition(to: size, with: coordinator)
scalingCarousel.deviceRotated()
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)
])
}
// 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()
viewWillTransition(to size:, with coordinator:)
方法中调用以下代码。如果在 viewDidLoad 中通过代码创建了 ScalingCarousel,则重要的是验证在调用方法 viewWillTransition
时它是否存在,否则如果在横向模式下加载 ViewController,我们将发生崩溃super.viewWillTransition(to: size, with: coordinator)
if scalingCarousel != nil {
scalingCarousel.deviceRotated()
}
要运行示例项目,请克隆存储库,然后首先从 Example 目录运行 pod install
。
iOS 10
ScalingCarousel 可通过 CocoaPods, Carthage 和 Swift Package Manager获得。
要通过 Cocoapods 安装,请将以下行添加到您的 Podfile
pod "ScalingCarousel"
要通过 Carthage 安装,请将以下行添加到您的 Podfile
github "superpeteblaze/ScalingCarousel"
要通过 Swift package manager 安装
注意:以下说明适用于在没有 Xcode UI 的情况下使用 SwiftPM。最简单的方法是转到您的 Project Settings -> Swift Packages 并从那里添加 ScalingCarousel。
要使用 Apple 的 Swift package manager 进行集成,无需 Xcode 集成,请将以下内容作为依赖项添加到您的 Package.swift
.package(url: "https://github.com/superpeteblaze/ScalingCarousel.git", .upToNextMajor(from: "3.2.0"))
Pete Smith, peadar81@gmail.com
ScalingCarousel 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。