Intro to SwiftUI Animation |

This is a companion discussion topic for the original entry at
1 Like

I loved this lesson! I noticed that we didn’t address making these animations happen for the reset button (and I’m guessing it will also be something to address again with leaderboards later). Might be a good idea to add a challenge in here to make the alert animate with the reset button (unless we are planning to disable the background buttons while the alert is up?)

I figured out how to animate and reset the whole view when pressing the reset button by applying what was taught but would be interested in the solution that would be presented in a challenge.

Thanks @jnubz! And great idea about adding animations for the reset button, I never thought of that. That does sound like a good challenge!

1 Like

I removed the .transition() from the VStack in PointsView() since there is also a .transition() on PointsView() in ContentView.swift and the animation still looks exactly the same.

1 Like

Hi Ray, thanks for this wonderful lesson. I ran into one syntax issue when using ternary expression:

RoundedRectangle(cornerRadius: 20.0)
    .frame(width: wideShapes ? 200 : 100, height: 100)

When I compile, it is just fine and succeeded, but when I try to use the preview canvas, it shows this error:

Compiling failed: result values in '? :' expression have mismatching types 'Int' and 'CGFloat'

My XCode version: 12.5 (12E262)

Tentative solution

Explicitly cast the type to CGFloat:

RoundedRectangle(cornerRadius: 20.0)
    .frame(width: CGFloat(wideShapes ? 200 : 100), height: 100)

This works but not a good approach I think.

@zhongdongy I’m seeing the same behavior with Xcode 12.5. It seems that before this version of Xcode, the compiler would automatically convert an Int (or Double) to a CGFloat, and now you have to cast it manually.

Even though the code looks a bit uglier this way, I think your current solution is fine.

1 Like

Hi Ray @rwenderlich , thanks for your reply. And I also find an implemented proposal of Swift here: swift-evolution/ at main · apple/swift-evolution ( Looks like we can just use Float / Double in Swift 5.5.

I noticed this as well. Did you figure out why?

You set modifier “.transition(…)” at body in PointsView struct and PointsView instance in ContentView body. Isn’t it duplicate?

Wrap around with “withAnimation{…}” alertIsVisible variable, find ContentView and PointsView and find other swift file… there’s more good way?