Giter Site home page Giter Site logo

standfordcs193p's Introduction

Standford193P

Card Game

Day 1( Lectures 1 ~ 4)


  • MVVM DESIGN PATTER

  • STRUCTURES AND CLASSES
  • Similarities:
    • Pretty much exactly the same syntax
  • Difference between Structs and Class
    • CLASS :
      1. Reference Type
      2. Passed Around with pointers
      3. Object Oriented Programming( Only single inheritence is available)
      4. Free Initializers.
      5. Always mutabale
      6. Used in View Model with Observable Object Protocol when using the MVVM architechture
    • STRUCT:
      1. Value Types
      2. Coppies when passed or assigned
      3. Copy on write
      4. Functional Programming
      5. No Inheritence
      6. Mutability must be explicitely stated

  • GENERICS:
    • We use generics when we dont know what type of data we want to take in.
    • Arrays are perfect examples.
       Struct Array<Element> { 
          func append(_ element: Element) {...}
      
        }
    • Array implementation of append knows nothing about the argument and it does not care. Element is not any known struct or class of protocol, it is just a placeholder.

  • CLOSURES:
    • It is so commin to pass functions around that we are very often in lining them.
    • We call such an inclined function a closure and there is a special language to support for it.
    • let payment = { (user: String) -> Bool in
        print("Paying \(user)")
        return true
        }

Day 2( Lectures 5)

  • Access Control
    • almost every function or var used inside classes should be marked as private or private(set)

  • Computed Properties
    • provided by classes, structures, and enumerations.
    • Use
      1. It depends on other properties
      2. You’re defining the property inside an extension
      3. The property is an access point to another object without disclosing it fully
    • A Computed Property provides a getter and an optional setter to indirectly access other properties and values. It can be used in several ways.
    private var indexOfTheOneAndOnlyFaceUpCard: Int? {
        get{
            cards.indices.filter({cards[$0].isFacedUp}).oneAndOnly
        }
        set{
            cards.indices.forEach({cards[$0].isFacedUp = ($0 == newValue)})
        }
        
    }

  • Extensions
    • Functionality of an existing class, structure or enumeration type can be added with the help of extensions.
    • extension Array{
          var oneAndOnly: Element? {
              if self.count == 1{
                  return self.first
              } else{
                  return nil
              }
          }
      

  • Property Observers
    • Detect changes in structs willSet and didSet
    • newValue -> a special variable(the value is going to get set to)
    • oldValue -> what the value used to be
    • willSet
    • didSet

  • Layout
    • Hstack, VStack, LazyVStack, LazyHStack, scrollView, LazyHGrid, LazyVGrid, aspectRatio, padding, GoemetryReader, List, Form, OutlineGroup

Day 3(Assignment 2 and Lectures 5)

  • Protocols
    • almost every function or var used inside classes should be marked as private or private(set)

  • @ViewBuilder
    • ViewBuilder is used extensively in SwiftUI to let you create new on-screen views by just listing them out in a trailing closure
    • It's a property wrapper applied to function parameter
    • extension Array{
          var oneAndOnly: Element? {
              if self.count == 1{
                  return self.first
              } else{
                  return nil
              }
          }
      

  • @escaping
    • @escaping is used to inform callers of a function that takes a closure that the closure might be stored or otherwise outlive the scope of the receiving function
    • whenever you have a function that you are passing that escapes. mark it @escaping. We are holding onto the function.
    •  extension Array{
           var oneAndOnly: Element? {
               if self.count == 1{
                   return self.first
               } else{
                   return nil
               }
           }
      

Day 4(Lectures 6-8)

  • Implicit Aninamtions
    • .animation(Animation)
    • Automatic animation
    • All view modifiers that precede this call will be animated
    • The changes are modified with duration code
    • Text(card.content)
                    .rotationEffect(Angle.degrees(card.isMatched ? 360 : 0))
                    .animation(Animation.linear(duration: 0.5), value:card.isMatched)
                    .font(font(in: geometry.size))
  • Explicit Animations
    • Explicit Animations are not view modifers.
    • They are functions
    • Animates the whole content that you are callling
    • Button("Shuffle"){
                withAnimation{
                    game.shuffle()
                }
            }
  • Trasitions
    • Transitions specify how to animate the arrival and departure of Views
    • Only works for views that are inside CTAAOS *(Containers That Are Already On Screen)
    • ForEach(game.cards.filter(isUndealt)){ card in
                cardView(card: card)
                    //.matchedGeometryEffect(id: card.id, in: dealingNameSpace)
                    .transition(AnyTransition.asymmetric(insertion: .opacity, removal: .identity))
                    .zIndex(zIndex(of:card))
                
            }
  • Animatable Modifier
    • View Modifier to do Animations
    • struct Cardify:AnimatableModifier{
       var isFacedUp: Bool
       var isMatched: Bool
       var rotation: Double //rotation in degrees
      
       var animatableData: Double{
           get{rotation}
           set{rotation = newValue}
       }
      
      
  • matchedGeometryEffect and @NameSpace
    • Sometimes you want a view to move from one place of the screen to another
    • If the view is moving to a new place in its same container, this is no problem
    • Moving like this is just animating the .position ViewModifier arguments
    • This kind of thing happens automatically when you explicitly animate
    • struct EmojiMemoryGameView: View {
           @ObservedObject var game: emojiMemoryGame
           @Namespace private var dealingNameSpace
           
            ForEach(game.cards.filter(isUndealt)){ card in
                 cardView(card: card)
                     .matchedGeometryEffect(id: card.id, in: dealingNameSpace)
                     .transition(AnyTransition.asymmetric(insertion: .opacity, removal: .identity))
                     .zIndex(zIndex(of:card))
                 
             }
             
  • Using onAppear for geometry
    • use when you want to animate a view when it first appears on screen use .onAppar.
    •  Pie(startAngle: Angle(degrees: 0-90), endAngle: Angle(degrees: (1-animatedBonusRemaing)*360-90))
        .onAppear {
            animatedBonusRemaing = card.bonusRemaining
            withAnimation(.linear(duration: card.bonusTimeRemaining)) {
                animatedBonusRemaing = 0
            }
        }

standfordcs193p's People

Contributors

eyucherin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.