article

Monday, July 5, 2021

SwiftUI Simple Transition opacity slide and scale

SwiftUI Simple Transition opacity slide and scale ContentView.swift
 
//
//  ContentView.swift
//  Test
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    
    @State private var show = false
    
    var body: some View {
        
        VStack {
            Spacer()
        
            if show {
                LabelView()
                    .animation(.easeInOut(duration: 1.0))
                    //.transition(.opacity)
                    //.transition(AnyTransition.opacity.combined(with: .slide))
                    .transition(.asymmetric(insertion: AnyTransition.opacity.combined(with: .slide), removal: .scale))
            }
            
            Spacer()
            
            Button("Animate") {
                self.show.toggle()
            }.padding(20)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct LabelView: View {
    var body: some View {
        Text("Animate Button")
            .padding(10)
            .font(.title)
            .foregroundColor(.white)
            .background(RoundedRectangle(cornerRadius: 8).fill(Color.green).shadow(color: .gray, radius: 3))
    }
}

Related Post