article

Monday, July 5, 2021

SwiftUI Simple Transition opacity slide and scale

SwiftUI Simple Transition opacity slide and scale ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
//  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