Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Lankinen
Lankinen

Posted on

     

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 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
vartheGesture:someGesture{returnTapGesture(count:2).onEnded{}}

non-discrete gestures

To see end result

vartheGesture:someGesture{DragGesture(...).onEnded{valuein...}}

To see the value of gesture

@GestureStatevarmyGestureState:MyGestureStateType=<startingvalue>

Returns<starting value> always when the gesture ends.

vartheGesture:someGesture{DragGesture(...).updating($myGestureState){value,myGestureState,transactioninmyGestureState=/* usually something related to value */}.onEnded{valuein/* do something */}}

myGestureState can be modified only inside.updating

vartheGesture:someGesture{DragGesture(...).onChanged{valuein/* do something with value (which is the state of the fingers) */.onEnded{valuein/* 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
varjson:Data?{returntry?JSONEncoder().encode(self)}init?(json:Data?){ifjson!=nil,letnewEmojiArt=try?JSONDecoder().decode(EmojiArt.self,from:json!){self=newEmojiArt}else{returnnil}}

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

....gesture(self.doubleTapToZoom(in:geometry.size))...privatefuncdoubleTapToZoom(insize:CGSize)->someGesture{TapGesture(count:2).onEnded{withAnimation{self.zoomToFit(self.document.backgroundImage,in:size)}}}privatefunczoomToFit(_image:UIImage?,insize:CGSize){ifletimage=image,image.size.width>0,image.size.height>0{lethZoom=size.width/image.size.widthletvZoom=size.height/image.size.heightself.zoomScale=min(hZoom,vZoom)}}

@RealLankinen

Originally published here

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Not ready...
  • Location
    Earth
  • Joined

More fromLankinen

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp