CS193p Notes — Lecture 8: Gestures JSON
UserDefaults
- Numerous ways to make data persists in iOS:
FileManager
filesystemCoreData
SQL databaseCloudKit
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 everywherelet 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 returnsArray<Any>
— To useAny
type it's possible to change the type withas
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