CS193p Notes - Lecture 8: Gestures JSON
Tags: ios, swift
Published: 2020-07-26 16:38:28

### UserDefaults

- Numerous ways to make data persists in iOS:
    - `FileManager` filesystem
    - `CoreData` SQL database
    - `CloudKit` database in the cloud
    - 3rd party options
    - `UserDefaults` simplest to use but only for lightweight data. Like dictionary.
- `UserDefaults` is most often used by making one instance and using it everywhere
    - `let defaults = UserDefaults.standard`
- `defaults.set(object, forKey: "SomeKey")`
    - `let i: Int = defaults.integer(forKey: "MyInteger")`
    - `let b: Data? = defaults.data(forKey: "MyData")`
    - `let a = array(forKey: "MyArray")` this returns `Array<Any>`
        - To use `Any` type it's possible to change the type with `as`

### Gestures ([8:37](https://youtu.be/mz-rNLWJ0bk?t=517))

- `myView.gesture(theGesture)`
- gesture is implemented in func or computer var

```swift
var theGesture: some Gesture {
  return TapGesture(count: 2).onEnded { }
}
```

non-discrete gestures

To see end result

```swift
var theGesture: some Gesture {
  DragGesture(...)
    .onEnded { value in ... }
}
```

To see the value of gesture

```swift
@GestureState var myGestureState: MyGestureStateType = <starting value>
```

Returns `<starting value>` always when the gesture ends.

```swift
var theGesture: some Gesture {
  DragGesture(...)
    .updating($myGestureState) { value, myGestureState, transaction in
      myGestureState = /* usually something related to value */
    }
    .onEnded { value in /* do something */ }
}
```

`myGestureState` can be modified only inside `.updating`

```swift
var theGesture: some Gesture {
  DragGesture(...)
    .onChanged { value in
      /* do something with value (which is the state of the fingers) */
    .onEnded { value in /* do something */ }
}
```

- `.updating` is better in most of the cases because you care only the relative change

### Demo ([22:21](https://youtu.be/mz-rNLWJ0bk?t=1341))

- `UserDefaults.standard.set(emojiArt.json, forKey: "EmojiArtDocument.Untitled")`
- `Codable` property means that struct can be encoded and decoded

```swift
var json: Data? {
  return try? JSONEncoder().encode(self)
}

init?(json: Data?) {
  if json != nil, let newEmojiArt = try? JSONDecoder().decode(EmojiArt.self, from: json!) {
    self = newEmojiArt
  } else {
    return nil
  }
}
```

Double tap makes zooms in a way that you can see the full image

```swift
...
.gesture(self.doubleTapToZoom(in: geometry.size))

...

private func doubleTapToZoom(in size: CGSize) -> some Gesture {
  TapGesture(count: 2)
    .onEnded {
      withAnimation {
        self.zoomToFit(self.document.backgroundImage, in: size)
      }
    }
}

private func zoomToFit(_ image: UIImage?, in size: CGSize) {
  if let image = image, image.size.width > 0, image.size.height > 0 {
    let hZoom = size.width / image.size.width
    let vZoom = size.height / image.size.height
    self.zoomScale = min(hZoom, vZoom)
  }
}
```

[@RealLankinen](https://twitter.com/reallankinen)

- [Notes 1](https://dev.to/lankinen/cs193p-notes-lecture-1-course-logistics-and-introduction-to-swiftui-44ap)
- [Notes 2](https://dev.to/lankinen/cs193p-notes-lecture-2-mvvm-and-the-swift-type-system-406f)
- [Notes 3](https://dev.to/lankinen/cs193p-notes-lecture-3-reactive-ui-protocols-layout-5g8a)
- [Notes 4](https://dev.to/lankinen/cs193p-notes-lecture-4-grid-enum-optionals-4noj)
- [Notes 5](https://dev.to/lankinen/cs193p-notes-lecture-5-viewbuilder-shape-viewmodifier-fgh)
- [Notes 6](https://dev.to/lankinen/cs193p-notes-lecture-6-animation-1a8f)
- [Notes 7](https://dev.to/lankinen/cs193p-notes-lecture-7-multithreading-emojiart-2ak3)
- [**Notes 8**](https://dev.to/lankinen/cs193p-notes-lecture-8-gestures-json-1f3m)
- [Notes 9](https://dev.to/lankinen/cs193p-notes-lecture-9-data-flow-26e)
- [Notes 10](https://dev.to/lankinen/cs193p-notes-lecture-10-navigation-textfield-3ei9)
- [Notes 11](https://dev.to/lankinen/cs193p-notes-lecture-11-picker-2hgh)
- [Notes 12](https://dev.to/lankinen/cs193p-notes-lecture-12-core-data-eci)
- [Notes 13](https://dev.to/lankinen/cs193p-notes-lecture-13-persistence-2jeh)
- [Notes 14](https://dev.to/lankinen/cs193p-notes-lecture-14-uikit-integration-1p7m)

[Originally published here](https://www.notion.so/lankinen/CS193p-2020-iOS-development-course-from-Stanford-81c094238e9d471bbf059ea8d83b3e15)