Tuesday, March 19, 2024
HomeiOS Developmentios - Implement slide animation of view when mannequin modifications in SwiftUI

ios – Implement slide animation of view when mannequin modifications in SwiftUI


When you simply wish to change the animation, it’s totally straightforward ୧(๑•̀⌄•́๑)૭

change the .transition(.slide) to .transition(.uneven(insertion: .transfer(edge: .main), elimination: .transfer(edge: .trailing)))

Showing Image

If there’s every other drawback, please confer with the next content material. If it actually doesn’t work, please be happy to ask me once more.

Instance Code:

import SwiftUI

struct ChooseTrainingView: View {
  var phrase: String
  var successHandler: (Bool) -> Void

  var physique: some View {
    ZStack {
      RoundedRectangle(cornerRadius: 25.0)
        .shadow(radius: 10)
        .body(width: 300, top: 400)
      
      VStack {
        HStack {
          Textual content(phrase)
            .font(.title)
            .foregroundColor(.white)
          Picture(systemName: "speaker.wave.2")
            .foregroundColor(.white)
        }
        
        Divider().background(Colour.white)
        
        VStack(alignment: .main, spacing: 10) {
          ForEach(0..<5) { index in
            Textual content("Possibility (index)")
              .padding(.vertical, 8)
              .padding(.horizontal)
              .foregroundColor(.white)
              .cornerRadius(10)
          }
        }
        .padding()
      }
      .padding()
    }
    .onTapGesture {
      self.successHandler(true)
      print("successHandler")
    }
  }
}

struct ContentView: View {
  @ObservedObject var coaching = TrainingModel(phrases: (0...5).map{Phrase(translation: String($0))})
  
  var physique: some View {
    ZStack {
      ForEach(coaching.phrases, id: .self) { phrase in
        if phrase == coaching.phrases[training.currentWordIndex] {
          ChooseTrainingView(phrase: phrase.translation, successHandler: { success in
            withAnimation {
              _ = coaching.goToNext(success: success)
            }
          })
          .id(phrase.id)
          .transition(.uneven(insertion: .transfer(edge: .main), elimination: .transfer(edge: .trailing)))
        }
      }
    }
  }
}

struct Phrase: Hashable, Identifiable {
  let id = UUID()
  var translation: String
}

class TrainingModel: ObservableObject {
  @Revealed var phrases: [Word]
  @Revealed var currentWordIndex: Int
  
  init(phrases: [Word]) {
    self.phrases = phrases
    self.currentWordIndex = 0
  }
  
  func goToNext(success: Bool) -> Bool {
    if currentWordIndex < phrases.rely - 1 {
      currentWordIndex += 1
      return true
    } else {
      return false
    }
  }
}

#Preview {
  ContentView()
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments