23.12.22 TIL SwiftUI 23. Adding Animations

Hay·2023년 12월 22일
0

SwiftUI_Beginner

목록 보기
19/19

Adding Animations

애니메이션을 적용하면 화면이 바로바로 바뀌는게 아니라 변환하는 과정이 애니메이션으로 보여진다

import SwiftUI

struct AnimationBootcamp: View {
    
    @State var isAnimated: Bool = false
    
    var body: some View {
        VStack {
            Button("Button") {
                //애니메이션 추가됨 ㄷㄷ
                withAnimation(
                    Animation
                        .default
                        .repeatForever(autoreverses: true)
                ) {
                    isAnimated.toggle()
                }
                
            }
            
            Spacer()
            //애니메이션 추가됨!!
            RoundedRectangle(cornerRadius: isAnimated ? 50 : 25)
                .fill(isAnimated ? Color.red : Color.green)
                .frame(
                    width: isAnimated ? 100 : 300,
                    height: isAnimated ? 100 : 300)
                .rotationEffect(Angle(degrees: isAnimated ? 360 : 0))
                .offset(y: isAnimated ? 300 : 0)
//               .animation(Animation
//                       .default
//                       .repeatForever(autoreverses: true))
                // 여기에 애니메이션을 넣을 수도 있지만, 위에서 withAnimation 사용하는 것을 ㅊㅊ
            Spacer()
        }
    }
}

#Preview {
    AnimationBootcamp()
}

0개의 댓글