Complete Day 92

This commit is contained in:
CypherPoet 2020-01-24 00:24:27 -06:00
parent e8da28c9c6
commit e93da24434
3 changed files with 97 additions and 6 deletions

View File

@ -97,12 +97,13 @@ I'm currently seeking freelance, remote opportunities as an iOS developer! If yo
- **Day 88:** [_Project 17: Flashzilla (Part Three)_](./day-088/)
- **Day 89:** [_Project 17: Flashzilla (Part Four)_](./day-089/)
- **Day 90:** [_Project 17: Flashzilla (Part Five)_](./day-090/)
- **Day 91:** [_Project 17: Flashzilla (Part Six)_](./day-091/)
</details>
**Latest Day:**
- **Day 91:** [_Project 17: Flashzilla (Part Six)_](./day-091/)
- **Day 92:** [_Project 18: Layout And Geometry (Part One)_](./day-092/)
@ -279,3 +280,10 @@ I'm currently seeking freelance, remote opportunities as an iOS developer! If yo
<br/>
<br/>
- [Project 17: Flashzilla](./day-086/Projects/Flashzilla/)
<br/>
<br/>

View File

@ -9,8 +9,7 @@
import SwiftUI
struct CustomAlignmentGuidesExample {
extension VerticalAlignment {
enum CustomAlignment: AlignmentID {
static func defaultValue(in dimensions: ViewDimensions) -> CGFloat {
dimensions[VerticalAlignment.center]
@ -22,14 +21,19 @@ struct CustomAlignmentGuidesExample {
}
struct CustomAlignmentGuidesExample {
}
// MARK: - View
extension CustomAlignmentGuidesExample: View {
var body: some View {
HStack(alignment: Self.customAlignment) {
HStack(alignment: .customAlignment) {
VStack {
Text("Twitter: @Cypher_Poet")
.alignmentGuide(Self.customAlignment) { dimensions in
.alignmentGuide(.customAlignment) { dimensions in
dimensions[VerticalAlignment.top]
}
@ -45,7 +49,7 @@ extension CustomAlignmentGuidesExample: View {
Text("HUMAN")
.font(.largeTitle)
.alignmentGuide(Self.customAlignment) { dimensions in
.alignmentGuide(.customAlignment) { dimensions in
dimensions[VerticalAlignment.bottom]
}

79
day-092/README.md Normal file
View File

@ -0,0 +1,79 @@
# Day 92: Project 18: _Layout And Geometry_ (Part One)
_Follow along at https://www.hackingwithswift.com/100/swiftui/92_.
<br/>
# 📒 Field Notes
This day covers Part One of _`Project 18`_ in the [100 Days of SwiftUI Challenge](https://www.hackingwithswift.com/100/swiftui/92).
It focuses on several specific topics:
- Layout and geometry: Introduction
- How layout works in SwiftUI
- Alignment and alignment guides
- How to create a custom alignment guide
## Layout and geometry: Introduction
From the project description:
> In this technique project were going to explore how SwiftUI handles layout.
>
> ...
>
> Along the way youll also learn about creating more advanced layout alignments, building special effects using GeometryReader, and more.
## How layout works in SwiftUI
1. A parent view proposes a size for its child.
2. Based on that information, the child then chooses its own size and the parent must respect that choice.
3. The parent then positions the child in its coordinate space.
A key insight to go along with this is that modifiers make containers. As such, every modifier becomes the parent of a child view its attached to, and this three-step process is performed anew.
## Alignment and alignment guides
### Alignment
A neat way to think of alignment is that it refers to how a view is pinned on its main axis.
For horizontal views, the main axis is a horizontal line, so the pinning refers to a vertical adjustment.
For vertical views, the main axis is a vertical line, so the pinning refers to a horizontal adjustment.
### Alignment Guides
Alignment guides are basically the way that views configure their layout _with respect_ to their current alignment. It makes sense once you start using them 😛.
A key insight to go along with this is that alignment guide computation is distinct from offsetting. Using the `offset` modifier will move a view relative to its container; alignment guides will compute the view's layout position -- and the container will be defined as the resulting perimeter.
## How to create a custom alignment guide
Exhibit A:
```swift
extension VerticalAlignment {
enum CustomAlignment: AlignmentID {
static func defaultValue(in dimensions: ViewDimensions) -> CGFloat {
dimensions[VerticalAlignment.center]
}
}
static let customAlignment = VerticalAlignment(CustomAlignment.self)
}
```