CS193p Notes - Lecture 6: Animation
Tags: ios, swift
Published: 2020-07-26 16:37:25

### Property Observers

```swift
var isFaceUp: Bool {
  willSet {
    if newValue {
      startUsingBonusTime()
    } else {
      stopUsingBonusTime()
    }
  }
}
```

- Syntax reminds computer var but it's not the same thing
- Most of the cases `newValue` contains the value it's going to get but in `didSet` the value is in `oldValue`

### @State ([1:47](https://youtu.be/3krC2c56ceQ?t=107))

- Views are read only
- Sometimes we want to keep some state and that happens by adding `@state` front of var
    - Case might be that we want to allow user to edit some image first and then the intention is called with all the state changes and it's stored to the model.
- Changing a variable with `@state` redraws the view

### Animation ([8:14](https://youtu.be/3krC2c56ceQ?t=494))

- Animation happens after the thing happened but in user's point of view it looks like happening the same time
    - The idea is that the value is changed once from A to B and animation is played instead of changing the values from A to B and drawing the animation after every change
- The point of animation is to draw attention to thing that change
- Changes to views in containers that are already on screen
    - The appearance and disappearance of Views
    - Changes to the arguments to Animatable view modifiers of Views
    - Changes to the arguments to the creation of Shapes
- Two ways to animate
    - Implicitly `.animation(Animation)`
        - "Automatic animation"

        ```swift
        Text("Ghost")
          .opacity(scary ? 1 : 0)
          .rotationEffect(Angle.degrees(upsideDown ? 180 : 0))
          .animation(Animation.easeInOut)
        ```

        - You can control
            - duration
            - delay
            - repeat
            - curve
                - rate at which the animation plays out
                    - `.linear`
                    - `.easeInOut`
                    - etc.
        - When added to a container, all the items inside it get's own animation instead of animating the whole container as one
    - Explicitly `withAnimation(Animation) { }`
        - Primary way of doing animation
        - This allows to do the same animation for a bunch of views
        - Explicit animations do not override an implicit animation

    - Transitions
        - Specify how to animate the arrival/departure of Views

        ```swift
        ZStack {
          if isFaceUp {
            RoundedRectangle()s
            Text("Ghost").transition(.scale)
          } else {
            RoundedRectangle(cornerRadius: 10).transition(.identity)
          }
        }
        ```

        - If `isFaceUp` changed (while ZStack is on screen and an explicit animation is in progress) ...
            - ... to false, the back would appear instantly, Text would shrink to nothing, front RR fade out.
            - ... to true, the back would disappear instantly, Text grow in from nothing, front RR fade in.
    - Places like if-else or for loop where the data changes are good for animations to highlight users the change.
    - Difference between Button and Text with OnTouch gesture is that different operating systems (WatchOS, iOS) know how to show it the best for the user

[@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)