swiftui-advanced
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSwiftUI Advanced
SwiftUI 高级进阶
Advanced SwiftUI patterns for gesture composition, adaptive layouts, architecture decisions, and performance optimization.
适用于手势组合、自适应布局、架构决策和性能优化的高级SwiftUI模式。
Reference Loading Guide
参考资料加载指南
ALWAYS load reference files if there is even a small chance the content may be required. It's better to have the context than to miss a pattern or make a mistake.
| Reference | Load When |
|---|---|
| Gestures | Composing multiple gestures, GestureState, custom recognizers |
| Adaptive Layout | ViewThatFits, AnyLayout, size classes, iOS 26 free-form windows |
| Architecture | MVVM vs TCA decision, State-as-Bridge, property wrapper selection |
| Performance | Instruments 26, view body optimization, unnecessary updates |
只要有哪怕一点点可能需要用到相关内容,就一定要加载参考文件。 拥有上下文总比遗漏模式或犯错要好。
| 参考资料 | 加载时机 |
|---|---|
| 手势 | 组合多个手势、GestureState、自定义识别器 |
| 自适应布局 | ViewThatFits、AnyLayout、尺寸类、iOS 26 自由形态窗口 |
| 架构 | MVVM vs TCA 决策、State-as-Bridge、属性包装器选择 |
| 性能 | Instruments 26、视图体优化、不必要的更新 |
Core Workflow
核心工作流程
- Identify pattern category from user's question
- Load relevant reference for detailed patterns and code examples
- Apply pattern following the decision trees and anti-patterns
- Verify using provided checklists or profiling guidance
- 从用户问题中识别模式类别
- 加载相关参考资料以获取详细模式和代码示例
- 遵循决策树和反模式应用对应模式
- 使用提供的检查清单或性能分析指南进行验证
Decision Trees
决策树
Gesture Composition
手势组合
- Both gestures at same time? ->
.simultaneously - One must complete before next? ->
.sequenced - Only one should win? ->
.exclusively
- 需要同时触发两个手势? ->
.simultaneously - 必须完成一个手势才能触发下一个? ->
.sequenced - 只能有一个手势生效? ->
.exclusively
Layout Adaptation
布局适配
- Pick best-fitting variant? ->
ViewThatFits - Animated H/V switch? ->
AnyLayout - Need actual dimensions? ->
onGeometryChange
- 需要选择最适合的布局变体? ->
ViewThatFits - 需要在横向/纵向布局间切换并带动画? ->
AnyLayout - 需要获取实际尺寸? ->
onGeometryChange
Architecture Selection
架构选择
- Small app, Apple patterns? -> @Observable + State-as-Bridge
- Complex presentation logic? -> MVVM with @Observable
- Rigorous testability needed? -> TCA
- 小型应用,遵循Apple官方模式? -> @Observable + State-as-Bridge
- 复杂的展示逻辑? -> 搭配@Observable的MVVM
- 需要严格的可测试性? -> TCA
Common Mistakes
常见误区
-
Gesture composition order matters —and
.simultaneouslyhave different trigger timing. Swapping them silently changes behavior. Understand gesture semantics before using..sequenced -
ViewThatFits over-used — ViewThatFits remeasures on every view change. For animated H/V switches, useinstead. Use ViewThatFits only for static variant selection.
AnyLayout -
onGeometryChange triggering unnecessary updates — Reading geometry changes geometry, which triggers updates, which changes geometry... circular. Useonly with proper state management to avoid loops.
.onGeometryChange -
Architecture mismatch mid-project — Starting with @Observable + State-as-Bridge then realizing you need TCA is expensive. Choose architecture upfront based on complexity (small app = @Observable, complex = TCA).
-
Ignoring view body optimization — Computing expensive calculations in view body repeatedly kills performance. Move calculations to properties or models. Profile with Instruments 26 before optimizing prematurely.
-
手势组合的顺序至关重要 —和
.simultaneously的触发时机不同。交换它们的顺序会悄无声息地改变行为。在使用前务必理解手势的语义。.sequenced -
过度使用ViewThatFits — ViewThatFits会在每次视图变化时重新测量。对于横向/纵向布局的动画切换,应使用替代。仅在选择静态布局变体时使用ViewThatFits。
AnyLayout -
onGeometryChange触发不必要的更新 — 读取几何信息会改变几何结构,进而触发更新,反过来又改变几何信息……形成循环。仅在配合恰当的状态管理时使用,以避免循环。
.onGeometryChange -
项目中途更换架构 — 先采用@Observable + State-as-Bridge,之后才发现需要TCA,这种转换成本很高。应根据项目复杂度提前选择架构(小型应用 = @Observable,复杂应用 = TCA)。
-
忽略视图体优化 — 在视图体中反复执行昂贵的计算会严重影响性能。将计算逻辑移至属性或模型中。在过早优化前,先使用Instruments 26进行性能分析。