StickyCollectionView-Swift 项目教程
1. 项目的目录结构及介绍
StickyCollectionView-Swift/
├── StickyCollectionView/
│ ├── Cells/
│ │ ├── StickyCollectionViewCell.swift
│ │ └── StickyCollectionViewHeader.swift
│ ├── Layouts/
│ │ ├── StickyLayout.swift
│ │ └── StickyLayoutAttributes.swift
│ ├── ViewControllers/
│ │ └── StickyCollectionViewController.swift
│ ├── AppDelegate.swift
│ ├── Assets.xcassets
│ ├── Info.plist
│ └── SceneDelegate.swift
├── StickyCollectionView.xcodeproj
├── StickyCollectionViewTests/
│ └── StickyCollectionViewTests.swift
└── StickyCollectionViewUITests/
└── StickyCollectionViewUITests.swift
目录结构介绍
- StickyCollectionView/: 主项目目录,包含所有源代码和资源文件。
- Cells/: 包含自定义的单元格和头部视图。
StickyCollectionViewCell.swift
: 自定义单元格类。StickyCollectionViewHeader.swift
: 自定义头部视图类。
- Layouts/: 包含自定义的布局类。
StickyLayout.swift
: 自定义的粘性布局类。StickyLayoutAttributes.swift
: 布局属性类。
- ViewControllers/: 包含视图控制器类。
StickyCollectionViewController.swift
: 主视图控制器类。
- AppDelegate.swift: 应用程序的入口和生命周期管理。
- Assets.xcassets: 资源文件,如图片等。
- Info.plist: 项目配置文件。
- SceneDelegate.swift: 场景代理类(适用于iOS 13及以上)。
- Cells/: 包含自定义的单元格和头部视图。
- StickyCollectionView.xcodeproj: Xcode 项目文件。
- StickyCollectionViewTests/: 单元测试目录。
StickyCollectionViewTests.swift
: 单元测试类。
- StickyCollectionViewUITests/: UI 测试目录。
StickyCollectionViewUITests.swift
: UI 测试类。
2. 项目的启动文件介绍
AppDelegate.swift
AppDelegate.swift
是应用程序的入口文件,负责管理应用程序的生命周期和全局状态。
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
SceneDelegate.swift
SceneDelegate.swift
是 iOS 13 及以上版本中引入的,负责管理应用程序的场景(窗口)。
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the