CS193p Notes — Lecture 8: Gestures JSON

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)

  • myView.gesture(theGesture)
  • gesture is implemented in func or computer var
var theGesture: some Gesture {
return TapGesture(count: 2).onEnded { }
}

non-discrete gestures

To see end result

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

To see the value of gesture

@GestureState var myGestureState: MyGestureStateType = <starting value>

Returns <starting value> always when the gesture ends.

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

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)

  • UserDefaults.standard.set(emojiArt.json, forKey: "EmojiArtDocument.Untitled")
  • Codable property means that struct can be encoded and decoded
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

...
.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)
}
}

https://twitter.com/reallankinen

Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5
Lecture 6
Lecture 7
Lecture 8
Lecture 9
Lecture 10
Lecture 11
Lecture 12
Lecture 13
Lecture 14

Originally published: https://www.notion.so/lankinen/Lecture-8-Gestures-JSON-e9b2c3b56c634b989d68829054649b0e

--

--

--

https://Lankinen.xyz https://twitter.com/reallankinen

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

macOS apps without storyboard or .xib menu bar in Swift 5 — Menubar and Toolbar

XCUITest with Xcode and Swift

VIPER Architecture Implementation in Swift

Event Loop, Run Loop, Programs with and without it

How to Create an AWS Serverless REST API in Swift

Make awesome iOS Application icon for 15 minutes

Context Menu, Alert and ActionSheet in SwiftUI

Network-cmds for jailbroken iOS 11

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lankinen

Lankinen

https://Lankinen.xyz https://twitter.com/reallankinen

More from Medium

Everyone can code — Loop jumper

How the Fleksy SDK is distributed using the Swift Package Manager

Making a Cocoapod in Swift with an Objective-C .framework dependency

Continuously Building and Hosting our Swift DocC Documentation using Github Actions and Netlify