由Airbnb启发的轻量级自定义集合视图。
屏幕截图
要求
ASCCollectionView 版本
最小iOS目标 | Swift版本 | 5.x |
---|---|---|
1.4.0 | 10.0 | 3.x |
1.1.0 | 9.0 | 4.2 |
1.0.9 | 9.0 | 4.1 |
1.0.8 | 9.0 | 4.0 |
1.0.7 | 9.0 | 2.x |
1.0.2 | 8.0 | 2.x |
CocoaPods
CocoaPods是Cocoa项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用CocoaPods将ASCCollectionView集成到Xcode项目中,请在Podfile中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'ASCollectionView', '1.4.0'
end
然后,运行以下命令
$ pod install
Carthage
Carthage 是一个去中心化的依赖管理器,用于构建您的依赖项并提供二进制框架。
您可以使用以下命令使用 Homebrew 安装 Carthage
brew update
brew install carthage
要使用 Carthage 将 ASCollectionView 集成到您的 Xcode 项目中,请在其 Cartfile 中指定它。
github "abdullahselek/ASCollectionView" ~> 1.4.0
运行 carthage update 以构建框架,并将构建的 ASCollectionView.framework 拖放到您的 Xcode 项目中。
Swift 包管理器
修改您的 Package.swift 文件以包含以下依赖项
.package(url: "https://github.com/abdullahselek/ASCollectionView.git", from: "1.4.0")
运行 swift package resolve
XCFramework
XCFrameworks 需要 Xcode 11 或更高版本,系统集成与 .framework 格式的集成非常类似。请使用脚本 scripts/build-framework.sh 生成 ASCollectionView.xcframework 二进制存档,您可以用作 Xcode 中的依赖项。
ASCollectionView.xcframework 是一个发布(优化)的二进制文件,提供了最佳可用的 Swift 代码性能。
示例用法
demo 文件夹中有一个示例 viewcontroller,下面添加了一些示例代码。
您可以将 collectionview 添加到 storyboard、xib 文件或程序化添加,然后设置约束。回到您的 viewcontroller 并实现自定义的数据源和方法。
class ViewController: UIViewController, ASCollectionViewDataSource, ASCollectionViewDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
collectionView.registerNib(UINib(nibName: collectionElementKindHeader, bundle: nil), forSupplementaryViewOfKind: collectionElementKindHeader, withReuseIdentifier: "header")
collectionView.delegate = self
collectionView.asDataSource = self
}
// MARK: ASCollectionViewDataSource
func numberOfItemsInASCollectionView(asCollectionView: ASCollectionView) -> Int {
return numberOfItems
}
func collectionView(asCollectionView: ASCollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let gridCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! GridCell
gridCell.label.text = NSString(format: "Item %ld ", indexPath.row) as String
gridCell.imageView.image = UIImage(named: NSString(format: "image-%ld", indexPath.row % 10) as String)
return gridCell
}
func collectionView(asCollectionView: ASCollectionView, parallaxCellForItemAtIndexPath indexPath: NSIndexPath) -> ASCollectionViewParallaxCell {
let parallaxCell = collectionView.dequeueReusableCellWithReuseIdentifier("parallaxCell", forIndexPath: indexPath) as! ParallaxCell
parallaxCell.label.text = NSString(format: "Item %ld ", indexPath.row) as String
parallaxCell.updateParallaxImage(UIImage(named: NSString(format: "image-%ld", indexPath.row % 10) as String)!)
return parallaxCell
}
func collectionView(asCollectionView: ASCollectionView, headerAtIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(ASCollectionViewElement.Header, withReuseIdentifier: "header", forIndexPath: indexPath)
return header
}
func loadMoreInASCollectionView(asCollectionView: ASCollectionView) {
if numberOfItems > 30 {
collectionView.enableLoadMore = false
return
}
numberOfItems += 10
collectionView.loadingMore = false
collectionView.reloadData()
}
示例中使用的 GridCell collectionview 单元格
class GridCell: UICollectionViewCell {
@IBOutlet var label: UILabel!
@IBOutlet var imageView: UIImageView!
}
示例中使用的 ParallaxCell
class ParallaxCell: ASCollectionViewParallaxCell {
@IBOutlet var label: UILabel!
}
MIT 许可证 (MIT)
Copyright (c) 2016 Abdullah Selek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
致谢
Inspired by Airbnb and ninjaprox. Improved and all coded in new programming
language Swift.