PagerKit Skill
Overview
This skill provides expert guidance on PagerKit, a powerful SwiftUI library for creating highly customizable and cross-platform page-based navigation. It covers everything from basic usage and dynamic page generation to advanced customization of page indicators, event handling, and best practices. Use this skill to help developers effectively implement flexible and visually rich paging experiences in their SwiftUI applications across all Apple platforms.
Agent Behavior (Follow These Rules)
- 1. Clarify Paging Requirements: Always ascertain the user's specific needs regarding page content, indicator style, navigation flow, and platform targets before offering solutions.
- Prioritize idiomatic SwiftUI: Favor PagerKit's
PKPageBuilder and ForEach for declarative page construction, aligning with SwiftUI's design principles. - Platform-Specific Advice: When discussing indicator images, progress, or
UIPageViewController options, always specify platform availability and correct type (UIImage vs. Image, UIPageControlProgress). - Emphasize Modifiers: Direct users to the relevant
PKPagesView or PKPage modifiers for customization, using full modifier signatures (e.g., .pkPageNavigationOrientation(_:)). - Contextual Code Examples: Provide concise code snippets that illustrate the recommended usage within a
PKPagesView or PKPage context. - Highlight Cross-Platform: When possible, remind users of PagerKit's cross-platform consistency and how to handle platform-specific differences using
#if os(...) directives.
Project Settings
PagerKit's behavior is influenced by the project's deployment targets and Swift version.
- - Deployment Targets: PagerKit supports iOS 14.0+, iPadOS 14.0+, macOS 14.0+, tvOS 14.0+, visionOS 1.0+, and watchOS 10.0+. Some features (e.g.,
UIPageControlProgress) are only available on specific platforms and OS versions. - Swift Version: Requires Swift 5.9+.
If these are unknown, ask the developer to confirm them, especially when discussing platform-specific features.
Quick Decision Tree
When a developer needs PagerKit guidance, follow this decision tree:
- 1. Setting up a new pager?
* For basic installation and concepts →
references/PagerKit.md
* To define the overall pager structure →
references/PKPagesView.md
* To create individual page content → INLINECODE16
- 2. Generating pages dynamically from data?
* Using a collection of items → INLINECODE17
- 3. Controlling page flow or structure?
* Adding conditional pages (if/else) →
references/PKPageBuilder.md
* Setting horizontal or vertical navigation →
references/PKPagesView.md (
.pkPageNavigationOrientation)
- 4. Customizing the page indicator (dots)?
* Changing color (active/inactive) →
references/PKPagesView.md (
.pkPageControlIndicatorTintColor,
.pkPageControlIndicatorCurrentIndicatorTintColor)
* Changing background style (minimal, prominent, automatic) →
references/PKPageControlBackgroundStyle.md
* Adjusting position or spacing →
references/PKPagesView.md (
.pkPageControlIndicatorAlignment,
.pkPageControlPadding)
* Setting layout direction (e.g., vertical alignment) →
references/PKPageControlDirection.md
* Using custom images (global or per-page) →
references/PKPagesView.md,
references/PKPage.md
* Hiding the indicator (always or for single page) → INLINECODE31
- 5. Handling page change events or state?
* Binding to the current page index →
references/PKPagesView.md (
.pkCurrentPageIndex)
* Reacting to manual page changes →
references/PKPagesView.md (
.pkOnManualPageChange)
* Reacting to automatic page changes →
references/PKPagesView.md (
.pkOnAutoPageChange)
* Identifying page transition direction →
references/PKPageDirection.md
* Actions on transition start/end → INLINECODE39
- 6. Customizing individual page behavior?
* Setting automatic transition duration →
references/PKPage.md (
.pkPageDuration)
* Adding a custom footer to a page →
references/PKPage.md (
.pkPageFooter)
Triage-First Playbook
- - "My pages are not showing or look incorrect."
* Verify
PKPagesView contains valid
PKPage instances. Refer to
references/PKPagesView.md,
references/PKPage.md.
* If using dynamic content, check
ForEach implementation. Refer to
references/ForEach.md.
- - "The page indicator is not positioned or styled correctly."
* Examine
.pkPageControlIndicatorAlignment,
.pkPageControlIndicatorBackgroundStyle,
.pkPageControlIndicatorDirection modifiers on
PKPagesView. Refer to
references/PKPagesView.md,
references/PKPageControlBackgroundStyle.md,
references/PKPageControlDirection.md.
- - "I want to change the color of the active dot, but it's not working."
* Ensure
.pkPageControlIndicatorCurrentIndicatorTintColor(_:) is used on
PKPagesView. Refer to
references/PKPagesView.md.
- - "Pages are not transitioning automatically."
* Check if
.pkPageDuration(_:) is applied to the individual
PKPages with a non-nil duration. Refer to
references/PKPage.md.
- - "My conditional logic (
if statements) inside PKPagesView is giving compiler errors."
* Review
PKPageBuilder concepts, ensuring all branches return valid
PKPage components. Refer to
references/PKPageBuilder.md.
- - "How can I tell if the user swiped forward or backward?"
* Use the
PKPageDirection parameter in
.pkOnManualPageChange. Refer to
references/PKPagesView.md,
references/PKPageDirection.md.
Core Patterns Reference
Basic Pager Setup
CODEBLOCK0
Dynamic Pages with ForEach
CODEBLOCK1
Custom Page Indicator Styling
CODEBLOCK2
Handling Page Change Events
CODEBLOCK3
Integration Quick Guide
PagerKit is integrated via Swift Package Manager.
- 1. Add Package Dependency: In Xcode, go to File > Add Package Dependency and enter
https://github.com/SzpakKamil/PagerKit.git. - Import:
import PagerKit in your Swift files. - Deployment Targets: Ensure your project targets iOS 14.0+, iPadOS 14.0+, macOS 14.0+, tvOS 14.0+, visionOS 1.0+, or watchOS 10.0+ (Swift 5.9+).
For detailed setup, see references/PagerKit.md.
Reference Files
Load these files as needed for specific topics:
- -
PagerKit.md - General overview, setup, and core benefits. PKPagesView.md - Detailed information on the main pager container and its global modifiers.PKPage.md - Information on individual page creation and page-specific modifiers.ForEach.md - How to generate pages from collections of data.PKPageBuilder.md - Understanding the declarative content building for PKPagesView.PKPageControlBackgroundStyle.md - Options for the background style of the page indicator.PKPageControlDirection.md - Options for the layout direction of the page indicator dots.PKPageDirection.md - Understanding the direction of page transitions._index.md - A comprehensive index for all PagerKit reference documentation.
Best Practices Summary
- 1. Embrace Declarative UI: Use
PKPageBuilder with ForEach for flexible and maintainable page construction. - Customize Thoughtfully: Leverage the extensive modifier API to match native platform aesthetics and app branding, avoiding over-customization that hinders usability.
- Manage Pager State: Always bind
pkCurrentPageIndex to external state (@State or @Binding) for programmatic control and observation. - Implement Event Handling: Utilize callbacks (e.g.,
.pkOnManualPageChange, .pkOnTransitionEnd) for analytics, haptic feedback, or custom logic in response to navigation. - Mind Platform Differences: Be aware of modifiers and features that behave differently or are only available on specific Apple platforms and OS versions.
- Prioritize Accessibility: Ensure custom indicators and footers remain accessible (e.g., with VoiceOver support).
Note: This skill is based on the comprehensive documentation for PagerKit. For further details, visit the official documentation at documentation.kamilszpak.com/documentation/pagerkit/ or the project website at kamilszpak.com/pl/pagerkit.
PagerKit 技能
概述
本技能提供关于 PagerKit 的专业指导,这是一个强大的 SwiftUI 库,用于创建高度可定制且跨平台的基于页面的导航。它涵盖了从基本用法和动态页面生成到页面指示器的高级定制、事件处理以及最佳实践的所有内容。使用本技能可帮助开发者在所有 Apple 平台的 SwiftUI 应用中有效实现灵活且视觉丰富的分页体验。
代理行为(遵循以下规则)
- 1. 明确分页需求: 在提供解决方案之前,始终确认用户在页面内容、指示器样式、导航流程和平台目标方面的具体需求。
- 优先使用地道的 SwiftUI: 优先使用 PagerKit 的 PKPageBuilder 和 ForEach 进行声明式页面构建,遵循 SwiftUI 的设计原则。
- 平台特定建议: 在讨论指示器图像、进度或 UIPageViewController 选项时,始终指定平台可用性和正确的类型(UIImage 与 Image、UIPageControlProgress)。
- 强调修饰符: 引导用户使用相关的 PKPagesView 或 PKPage 修饰符进行定制,使用完整的修饰符签名(例如 .pkPageNavigationOrientation(_:))。
- 上下文代码示例: 提供简洁的代码片段,说明在 PKPagesView 或 PKPage 上下文中的推荐用法。
- 突出跨平台性: 在可能的情况下,提醒用户 PagerKit 的跨平台一致性,以及如何使用 #if os(...) 指令处理平台特定差异。
项目设置
PagerKit 的行为受项目部署目标和 Swift 版本的影响。
- - 部署目标: PagerKit 支持 iOS 14.0+、iPadOS 14.0+、macOS 14.0+、tvOS 14.0+、visionOS 1.0+ 和 watchOS 10.0+。某些功能(例如 UIPageControlProgress)仅在特定平台和操作系统版本上可用。
- Swift 版本: 需要 Swift 5.9+。
如果这些未知,请要求开发者确认,尤其是在讨论平台特定功能时。
快速决策树
当开发者需要 PagerKit 指导时,遵循此决策树:
- 1. 正在设置新的分页器?
* 基本安装和概念 → references/PagerKit.md
* 定义整体分页器结构 → references/PKPagesView.md
* 创建单个页面内容 → references/PKPage.md
- 2. 从数据动态生成页面?
* 使用项目集合 → references/ForEach.md
- 3. 控制页面流程或结构?
* 添加条件页面(if/else) → references/PKPageBuilder.md
* 设置水平或垂直导航 → references/PKPagesView.md(.pkPageNavigationOrientation)
- 4. 自定义页面指示器(圆点)?
* 更改颜色(活动/非活动) → references/PKPagesView.md(.pkPageControlIndicatorTintColor、.pkPageControlIndicatorCurrentIndicatorTintColor)
* 更改背景样式(简约、突出、自动) → references/PKPageControlBackgroundStyle.md
* 调整位置或间距 → references/PKPagesView.md(.pkPageControlIndicatorAlignment、.pkPageControlPadding)
* 设置布局方向(例如垂直对齐) → references/PKPageControlDirection.md
* 使用自定义图像(全局或每页) → references/PKPagesView.md、references/PKPage.md
* 隐藏指示器(始终或单页时) → references/PKPagesView.md
- 5. 处理页面更改事件或状态?
* 绑定到当前页面索引 → references/PKPagesView.md(.pkCurrentPageIndex)
* 响应手动页面更改 → references/PKPagesView.md(.pkOnManualPageChange)
* 响应自动页面更改 → references/PKPagesView.md(.pkOnAutoPageChange)
* 识别页面转换方向 → references/PKPageDirection.md
* 转换开始/结束时的操作 → references/PKPagesView.md
- 6. 自定义单个页面行为?
* 设置自动转换持续时间 → references/PKPage.md(.pkPageDuration)
* 向页面添加自定义页脚 → references/PKPage.md(.pkPageFooter)
分诊优先剧本
* 验证 PKPagesView 包含有效的 PKPage 实例。参考 references/PKPagesView.md、references/PKPage.md。
* 如果使用动态内容,检查 ForEach 实现。参考 references/ForEach.md。
* 检查 PKPagesView 上的 .pkPageControlIndicatorAlignment、.pkPageControlIndicatorBackgroundStyle、.pkPageControlIndicatorDirection 修饰符。参考 references/PKPagesView.md、references/PKPageControlBackgroundStyle.md、references/PKPageControlDirection.md。
* 确保在 PKPagesView 上使用了 .pkPageControlIndicatorCurrentIndicatorTintColor(_:)。参考 references/PKPagesView.md。
* 检查是否在单个 PKPage 上应用了非 nil 持续时间的 .pkPageDuration(_:)。参考 references/PKPage.md。
- - 我在 PKPagesView 内部的条件逻辑(if 语句)导致编译器错误。
* 查看 PKPageBuilder 概念,确保所有分支都返回有效的 PKPage 组件。参考 references/PKPageBuilder.md。
* 在 .pkOnManualPageChange 中使用 PKPageDirection 参数。参考 references/PKPagesView.md、references/PKPageDirection.md。
核心模式参考
基本分页器设置
swift
PKPagesView {
PKPage { Text(页面 A).font(.title) }
PKPage { Text(页面 B).font(.title) }
PKPage { Text(页面 C).font(.title) }
}
.pkCurrentPageIndex(index: $currentPage) // 绑定到 @State
.pkPageNavigationOrientation(.horizontal)
使用 ForEach 的动态页面
swift
struct Item: Identifiable {
let id = UUID()
let title: String
}
// ... 在 View 内部
let items = [Item(title: 项目 1), Item(title: 项目 2)]
PKPagesView {
ForEach(items) { item in
PKPage { Text(item.title) }
.pkPageFooter { Text(\(item.title) 的页脚) }
}
}
自定义页面指示器样式
swift
.pkPageControlIndicatorAlignment(spacing: 10, alignment: .bottomTrailing)
.pkPageControlIndicatorBackgroundStyle(.prominent)
.pkPageControlIndicatorDirection(.topToBottom) // 垂直圆点
.pkPageControlIndicatorTintColor(.gray)
.pkPageControlIndicatorCurrentIndicatorTintColor(.blue)
// 自定义图像
#if os(iOS)
.pkPageControlIndicatorPreferredCurrentPageIndicatorImage(image: UIImage(systemName: star.fill))
#else
.pkPageControlIndicatorPreferredCurrentPageIndicatorImage(image: Image(systemName: star.fill))
#endif
处理页面更改事件
swift
.pkOnManualPageChange { currentIndex, direction in
print(用户通过 \(direction) 方向导航到页面 \(currentIndex)。)
}
.pkOnAutoPageChange { previousIndex, currentIndex in
print(从 \(previousIndex) 自动更改为 \(currentIndex)。)
}
.pkOnTransitionEnd { previous, current in
print(转换结束。之前是 \(previous),现在是 \(current)。)
}
集成快速指南
PagerKit 通过 Swift Package Manager 集成。
- 1. 添加包依赖: 在 Xcode 中,转到 文件 > 添加包依赖 并输入 https://github.com/SzpakKamil/PagerKit.git。
- 导入: 在 Swift 文件中使用 import PagerKit。
- 部署目标: 确保您的项目目标为 iOS 14.0+、iPadOS 14.0+、macOS 14.0+、tvOS 14.0+、visionOS 1.0+ 或 watchOS 10.0+(Swift 5.9+)。
有关详细设置,请参阅 references/PagerKit.md。
参考文件
根据需要加载这些