How to use Transition in SwiftUI | Bootcamp #27

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 95

  • @gibber1sh-c6w
    @gibber1sh-c6w Год назад +27

    To anyone that whose transition works only with the deprecated code. Here's what to do :
    1. Wrap the Button's `showView.toggle()` in a withAnimation() and you pass in the type of animation (.easeInOut) there
    Button("BUTTON") {
    withAnimation(.easeInOut){
    showView.toggle()
    }
    2. Remove the `.animation(.easeInOut)` from `RoundedRectangle()`. You don't need it. And keep only the `.transition(.slide)`

    • @ChromeBreakerD
      @ChromeBreakerD Год назад

      This does work, but is not really a solution.
      There may be scenarios in which using the "animation" modifier is prefered over the withAnimation() method.

    • @amandadias2078
      @amandadias2078 10 месяцев назад

      I LOVE YOUUUUUUUUUUUUUUU!!!!!!

    • @CMessineo
      @CMessineo 7 месяцев назад

      That worked - thanks!

    • @nancyjain6259
      @nancyjain6259 5 месяцев назад

      i tried using .animation(.easeInOut, value: showView), but not working as expected

    • @optimisticenigma283
      @optimisticenigma283 5 месяцев назад

      @@nancyjain6259 It doesn't work, removed it from the Rounded rectangle and instead putting the animation directly on the button as described by OP works.

  • @Stevesvideoshelf
    @Stevesvideoshelf 3 года назад +4

    My apps after watching this:
    Transitions
    Transitions Everywhere!
    Thanks again Nick! These videos are awesome!

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +1

      Haha yes, transitions everywhere! They are so powerful.

  • @SysAdminPgh
    @SysAdminPgh Год назад +3

    Works...
    Add a ZStack in the Previews
    struct TransitionBootcamp_Previews: PreviewProvider {
    static var previews: some View {
    ZStack {
    TransitionBootcamp()
    }
    }
    }
    Add your animation in the Button
    Button("Button") {
    withAnimation {
    showView.toggle()
    }
    }
    And the RoundedRectangle...
    if showView {
    RoundedRectangle(cornerRadius: 30)
    .frame(height: UIScreen.main.bounds.height * 0.5)
    .transition(.slide)
    }
    Seems 16.2 fixed a bunch of issues.

    • @richie_vo
      @richie_vo Год назад

      I was trying to find this answer everywhere. It works great, thank you!

  • @sA-ut3zb
    @sA-ut3zb 2 года назад +12

    For anyone having issues with the transitions only working on the outward (false) click, the issue is that there is a bug in the renderer for the Preview. A workaround for this is to embed your View in a VStack in the preview as below:
    struct TransitionBootcamp_Previews: PreviewProvider {
    static var previews: some View {
    VStack {
    TransitionBootcamp()
    }
    }
    }

    • @explosivevibe635
      @explosivevibe635 2 года назад

      tnx bro its help me)

    • @nikodemeroutis
      @nikodemeroutis 2 года назад

      @@explosivevibe635 thanks man ! helped me a lot !

    • @OlivierVanhamme
      @OlivierVanhamme Год назад +1

      This worked in combination with the withAnimation Button suggestion from John G.

    • @leezicai
      @leezicai Год назад

      Xcode 14.3.1 version have this issue, Playgrounds 4.3.1 is working, Simulator also working. Simulator need you change project's ContentView
      "struct ContentView: View {
      var body: some View {
      TransitionBootcamp()
      }
      }"

    • @hadidhsaputra
      @hadidhsaputra Год назад

      thanks bro it's helpful for me

  • @gibber1sh-c6w
    @gibber1sh-c6w Год назад +1

    @SwiftfulThinking regarding your question at 8:15: When you use .animation(.easeInOut, value: showView), you're specifying that the animation should only occur when the showView value changes. However, SwiftUI's transition modifiers, such as .transition, work based on view insertion and removal, not changes to a single property like showView.

  • @optimisticenigma283
    @optimisticenigma283 5 месяцев назад +4

    For anyone watching this with Xcode 15.4,
    Wrapping up the state variable in a "withAnimation()" potentially avoids all problems that you may run just following the tutorial, since the .animation modifier has been changed a bit.
    My code:
    ZStack (alignment : .bottom) {
    VStack {
    Button("BUTTON") {
    withAnimation{
    showView.toggle()
    }
    }
    Spacer()
    }
    if showView {
    RoundedRectangle(cornerRadius: 30)
    .frame(height: UIScreen.main.bounds.height * 0.5)
    .transition(.opacity)
    //.opacity(showView ? 1.0 : 0.0)
    //.animation(.easeInOut, value: showView)
    }
    }
    .ignoresSafeArea(edges: .bottom)

    • @vedantk23
      @vedantk23 4 месяца назад

      Why transition is not working when i called .animation modifier but using withAnimation it worked?

  • @harrywiseman6854
    @harrywiseman6854 Год назад +4

    In reference to the bug in Xcode mentioned below. It seems to be a problem with the canvas. If you add a VStack to your preview, the transitions will work as normal. For this tutorial, it would look something like this:
    }
    struct TransitionsBootcamp_Previews: PreviewProvider {
    static var previews: some View {
    ZStack {
    TransitionsBootcamp()
    }
    }
    }

    • @yoismak
      @yoismak Год назад +1

      Bruh I can't tell you how much time I spent searching for why my animation wasn't being applied on the transition. Thank you!

  • @profgallaugher
    @profgallaugher 2 года назад +4

    Enjoying the tutorials. Unless I'm missing something, I've encountered a bug in Xcode 13.3.1. The insertion transition (even on standard transitions without .asymmetric) is not showing in preview, but removal transition is. But when I ran in the simulator the transitions showed. Weird, but wanted to share in case anyone else saw this. Wasted a couple hours trying to run down the problem.
    Also - got around the depreciation problem with code below - note when the code below is run, the scale insertion transition doesn't run, but the scale does execute on removal. Running in the simulator, though, works fine:
    Button("Button") {
    withAnimation(.linear(duration: 0.5)) {
    showView.toggle()
    }
    }
    if showView {
    RoundedRectangle(cornerRadius: 30)
    .frame(height: UIScreen.main.bounds.height * 0.5)
    .transition(AnyTransition.scale)
    }

  • @AZMerf
    @AZMerf 3 года назад +7

    Woo Hoo! Half way through!

  • @huseyinsubhanalkan878
    @huseyinsubhanalkan878 Год назад +1

    Hi Nick thank you for this lectures. They are very helpful for me.🙂

  • @zlatkoiliev8927
    @zlatkoiliev8927 Год назад +2

    Hey Nick, really awesome series! I am learning a lot from you. One thing I noticed, it seems like with the latest version of xcode and swiftui things got changed, .animation is now deprecated and the suggestion is to either wrap the state value in withAnimation, or to add a second parameter to .animation(.spring(), value: ...) and I am not sure what the value should be here, I tried with showView, but this completely breaks the transition.

    • @Fvture-creates
      @Fvture-creates Год назад +1

      Yeah, I'm having the same issue

    • @optimisticenigma283
      @optimisticenigma283 5 месяцев назад

      The second one didn't work and instead wrapping the state value was the best solution for me here.

  • @luaneemiliano1041
    @luaneemiliano1041 2 года назад +2

    Hi Nick. You are a such good teacher, I've been learning so much with you and your videos, they are just so easy to understand and extremely well made. This bootcamp is definitely keeping me motivated to be a better developer every day more and more. I do have a question for you though, it is possible to animate SF symbols and icons ?? if so, how should I do it? Thank you so much and keep up the great work!!

    • @zacharyyong4095
      @zacharyyong4095 2 года назад +1

      Hi buddy, just simply change the rectangle to Image(systemName: "SF Symbol you want") and that should work. remember to add .resizable()

    • @luaneemiliano1041
      @luaneemiliano1041 2 года назад +1

      @@zacharyyong4095 Thank you!

  • @bbulliard
    @bbulliard 2 года назад +1

    Super awesome video

  • @erenunal1512
    @erenunal1512 2 месяца назад

    If I get a job, I promise I will buy you a lot of coffee with my first paycheck. You have my word!

  • @CMessineo
    @CMessineo 7 месяцев назад

    This is great, but definitely had to jump through some hoops to get it to work in 15.x

  • @gccount
    @gccount 8 месяцев назад

    Great video. IMO, transition is about direction, while animation is about timing (transition itself is always instant)

  • @bobearl99
    @bobearl99 2 года назад +1

    Fantastic series, Nick! For some reason, animating transitions appears to be problematic with the latest XCode beta 14: scale and opacity animations continue to work as you showed in your videos when. view transitions in, but move and slide do not. I tried adding AnyTransition as you showed at one point, but that did not solve it.
    For example, these modifiers work with your demo video:
    .transition(.asymmetric(
    insertion: .opacity,
    removal: .scale(scale: 0)).animation(.easeInOut)) // works
    .transition(.opacity.animation(.easeInOut)) // also works
    While these simply flash in without any animation:
    .transition(AnyTransition.move(edge: .bottom).animation(.default)) // fails
    I didn't see anything corresponding at StackOverflow, and I don't mean to give you a puzzle to solve, but I will be watching for your update to this video for ios15 to see if there's something simple I'm missing. Keep up the great work!

    • @DavidKrcek
      @DavidKrcek 2 года назад

      It really look like a "bug" in Xcode14 because removal works but insertion not.
      RoundedRectangle(cornerRadius: 20)
      .frame(maxHeight: UIScreen.main.bounds.size.height * 2 / 3)
      .transition(.asymmetric(
      insertion: .move(edge: .top).animation(Animation.easeIn),
      removal: .move(edge: .bottom).animation(Animation.easeOut)
      ))
      With this and
      Button {
      withAnimation {
      showView.toggle()
      }
      The removal works fine, but the insertion still pops in.
      Btw. Nick amazing job.

    • @DavidKrcek
      @DavidKrcek 2 года назад

      After digging in, it's an issue with the Canvas display inside Xcode 14
      If one using the simulor it works as supposed.

  • @mafiaboy16
    @mafiaboy16 2 года назад

    Hi Nick,
    after the update of the swiftui the comment came that the .animation will no longer be used in the future, that we have to work with withAnimations.
    There I discover an error or at least it doesn't work well for me. With the parameter .easeInOut, the slow entry of the subscreen comes in suddenly, but exiting the subscreen works fine. Do you have any tips on how I can tackle that problem?

  • @sadeqnoori8707
    @sadeqnoori8707 3 года назад +1

    Wow...Thanks man

  • @elemento_arts
    @elemento_arts Год назад

    Great tutorial Nick. Wondering how you got the parameters to color white on Xcode Settings? Thanks

  • @JeckyKA
    @JeckyKA 3 года назад

    amazing! thanks a lot!

  • @nicolasjurcak7979
    @nicolasjurcak7979 2 года назад

    You rock, man

  • @TeemoChan
    @TeemoChan Год назад +2

    I think You should re-upload this tutorial, the .slide is not working, animation is changed , and main will be deprecated

  • @MrSkyydude
    @MrSkyydude 2 года назад +2

    I'm running Xcode Version 13.4.1 that has the '' 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead" warning. I ran into trouble when adding the .transition(.slide) modifier at 5:47. No error, but it didn't perform correctly in the preview canvas. I noticed though if I use the .animation(.easeInOut) modifier w/ the warning instead of the .animation(.easeInOut, value: showView) w/o the warning then everything works as it should.

    • @nenudiana7642
      @nenudiana7642 Год назад

      thank you

    • @syahirsuyab
      @syahirsuyab Год назад

      same goes to me. running xcode 14.3.1
      using .animation(.easeIn, value: showView) does not work, however
      using .animation(.easeIn) does work but it gives the warning 'animation' was deprecated in iOS 15.0
      how can we use this transition without any warning?

    • @gibber1sh-c6w
      @gibber1sh-c6w Год назад +1

      @@syahirsuyab Here's what to do :
      1. Wrap the Button's `showView.toggle()` in a withAnimation() and you pass in the type of animation (.easeInOut) there
      Button("BUTTON") {
      withAnimation(.easeInOut){
      showView.toggle()
      }
      2. Remove the `.animation(.easeInOut)` from `RoundedRectangle()`. You don't need it. And keep only the `.transition(.slide)`

  • @andresraigoza2082
    @andresraigoza2082 2 года назад +3

    Thank you so much for this great content. I have a question though. When I do this examples with .animation(), I get this warning: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead. But if I add animation(_:value:) and I put showView in the value parameter, then the transition doesn't work. What would be the proper way to do this?

    • @SwiftfulThinking
      @SwiftfulThinking  2 года назад +6

      You can get rid of the .animation() and instead perform the action withAnimation. ...
      withAnimation(.easeInOut) {
      showView.toggle()
      }

    • @andresraigoza2082
      @andresraigoza2082 2 года назад +4

      ​@@SwiftfulThinking Thank you very much for your reply Nick. I did what you told me but the result is not what we expected. The animation only works when showView changes from true to false. This is my code:
      ZStack(alignment: .bottom) {
      VStack {
      Button("BUTTON:") {
      withAnimation(.easeInOut) {
      showView.toggle()
      }
      }
      Spacer()
      }
      if showView {
      RoundedRectangle(cornerRadius: 30)
      .frame(height: UIScreen.main.bounds.height * 0.5)
      .transition(.slide)
      }
      }
      .ignoresSafeArea(edges: .bottom)
      Am I doing something wrong here?

    • @surakadotme
      @surakadotme 2 года назад +4

      @@andresraigoza2082 hi, i think animations not working properly on canvas. try running the simulator instead. it worked for me. I will be very happy if you come back.

    • @andresraigoza2082
      @andresraigoza2082 2 года назад +1

      @@surakadotme Yes you are right!!! This seems to be a canvas bug, thank you so much 😀

    • @surakadotme
      @surakadotme 2 года назад

      @@andresraigoza2082 I'm really glad it worked :))

  • @ramazanluleci5586
    @ramazanluleci5586 2 года назад

    Hi Nick,
    I really like your bootcamp videos.
    I just thought of something in this video.
    Why did we add ZStack, I did it without adding it, it worked the same way.
    Is there anything I missed?

    • @SwiftfulThinking
      @SwiftfulThinking  2 года назад

      I think it was a bug on older iOS that may be fixed now

  • @natgenesis5038
    @natgenesis5038 2 года назад

    You are the best bro wow

  • @dmitrygolovin8066
    @dmitrygolovin8066 2 года назад

    Top Notch Video 👍

  • @knowledgeispower4953
    @knowledgeispower4953 9 месяцев назад

    Cool bro....😉

  • @Antonnel7
    @Antonnel7 2 года назад +1

    transition(.slider) is bug. How fix? Cotent not started of left side to right.

    • @Antonnel7
      @Antonnel7 2 года назад +1

      ZStack { VStack{...} ObjectView().transition(.slider) }.animation(.spirit) is great works!! ZStack need animated and object in stack need transition

  • @TeemoChan
    @TeemoChan Год назад

    it says .main will be deprecated in the future, What can We use instead?

  • @scoutmastert.7181
    @scoutmastert.7181 Год назад

    Hey, great vid. when i use .frame(height: UIScreen.main.bounds.height = 0.5)
    it throws two errors about height being a get only property and it not being able to convert () to CGFloat. Does this have to do with .main being deprecated?

    • @SwiftfulThinking
      @SwiftfulThinking  Год назад

      I think you mean “ * 0.5” and not “= 0.5”

    • @SwiftfulThinking
      @SwiftfulThinking  Год назад

      This error likely means you have a typo bc it thinks you added a function where it should be a CGFloat

    • @scoutmastert.7181
      @scoutmastert.7181 Год назад

      @@SwiftfulThinking oh wow thank you lmao

  • @奶爸学园
    @奶爸学园 2 года назад

    Thank you so much for this great video. When I do these examples with UIScene, I get this error: " Type 'UIScene' has no member 'main'", I using Xcode13.2, IOS15.2. How can I fix error?

  • @dglalperen
    @dglalperen 3 года назад

    Love your content bro !
    Do you have like a Discord Server or some where the community can ask questions or do some projects together ?
    thank you for all the hard work !

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +2

      Hey Alperen! I don't... yet. I'm planning on making one soon though! haha I don't have that many subs yet 🥺

    • @dglalperen
      @dglalperen 3 года назад

      @@SwiftfulThinkingstarting small while becoming a bigger Community 👍🏽😁

    • @hourglassentertainment237
      @hourglassentertainment237 3 года назад

      @@SwiftfulThinking Hey Nick, I watch your videos about every day, and every time I come on, there's at least another 200 subscribers, I think you had 2.7K When I found your channel! Whats the bet you're going to reach 10K by the New year???

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад

      @@hourglassentertainment237 Haha we're slowly growing 🙃

  • @mrclean851
    @mrclean851 3 года назад +1

    Nick, transition is working to slide in but not sliding out!!! Help

  • @hugob5093
    @hugob5093 3 года назад

    So transition works with an animation ? I tried to remove the animation, but the transition doesn't work.. This is the cases for all transitions ?
    And thank you for your amazing bootcamp !!

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +1

      Hi Hugo! Thanks for the comment :) and yes, transitions are animations that occur when a View is being drawn on or off the screen. We need to use transition with animation to make it work!

    • @amielterence
      @amielterence 3 года назад

      The transition modifier also comes with a withAnimation instance method.

  • @vladimirstepanov7246
    @vladimirstepanov7246 3 года назад

    top lessons)

  • @rahuljamba5846
    @rahuljamba5846 Год назад

    Grear

  • @raimitis
    @raimitis 2 года назад

    How can I have corner radius only on top and not bottom too. I use iPhone 8.

  • @dimflash1987
    @dimflash1987 2 года назад

    👍🔥

  • @TheSpiralnotizblock
    @TheSpiralnotizblock 2 года назад

    8:15 .opacity is not working on the canvas. In the simulator it works as expected

  • @shekhar_knox
    @shekhar_knox Год назад

    Hello Team,
    I was trying transition inside if condition, but its not showing effects..
    Anyone can help ?
    refer below code
    if showView {
    RoundedRectangle(cornerRadius: 10)
    .frame(height: UIScreen.main.bounds.height / 2)
    .transition(.slide)
    .animation(.easeInOut, value: showView)
    }

    • @optimisticenigma283
      @optimisticenigma283 5 месяцев назад

      Idk if you're still working on this but the ".animation" modifier doesn't work.
      Comment it and just embed the showView.toggle() inside a withAnimation() tag as follows:
      var body: some View {
      ZStack (alignment : .bottom) {
      VStack {
      Button("BUTTON") {
      withAnimation{
      showView.toggle()
      }
      }
      Spacer()
      }
      if showView {
      RoundedRectangle(cornerRadius: 30)
      .frame(height: UIScreen.main.bounds.height * 0.5)
      .transition(.slide)
      //.opacity(showView ? 1.0 : 0.0)
      //.animation(.easeInOut, value: showView)
      }
      }
      .ignoresSafeArea(edges: .bottom)
      }

  • @rasheed1andrew
    @rasheed1andrew 3 года назад

    how do you make your screen perform an automatic removal. more like a flash on the screen. like when you double tap on a video on instagram. the heart shows and disapears in about 1.5 seconds

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +1

      Hey Rasheed, the easiest solution for this would be to use a DispatchQueue with a delay, where you set "animate" to true and then after a few seconds delay, set "animate" to false.... or something like this.

  • @magedmohmed4403
    @magedmohmed4403 2 года назад

    Nick , I am in progress and the favor return to you , thank you 🤎🤎