SwiftUI3.0的Animation


//
//  ContentView.swift
//  SwiftUIAnimationHeight
//
//  Created by lujun on 2021/12/20.
//

import SwiftUI

struct ContentView: View {
    @State var enale: Bool = false
    var body: some View {
//        intro
//        implicit
//        explict
//        control
        complex
    }
    
    var intro: some View {
        VStack(alignment: .center, spacing: 10) {
            Text("Swift UI 高级进阶")
                .font(.title)
                .fontWeight(.bold)
            Text("Animation")
                .font(.title2)
                .fontWeight(.bold)
                .foregroundColor(.red)
                .scaleEffect(enale ? 2.0 : 1.0)
                .animation(.default.repeatForever())
        }.onAppear {
            enale = true
        }
    }
    
    var implicit: some View {
        VStack(spacing:50) {
            RoundedRectangle(cornerRadius: enale ? 100.0 : 0.0)
                .fill(enale ? Color.red : Color.green)
                .opacity(enale ? 1.0 : 0.3)
//                .animation(.default)
//                .animation(.linear)
//                .animation(.easeInOut)
//                .animation(.easeOut)
//                .animation(.linear(duration: 3))
//                .animation(.timingCurve(0.68, -0.6, 0.32, 1.6,duration: 2))
//                .animation(.spring())
//                .animation(.interactiveSpring(response: 0.7, dampingFraction: 200, blendDuration: 5))
//                .animation(.interpolatingSpring(mass: 0.7, stiffness: 200, damping: 5, initialVelocity: 4))
                .animation(.interactiveSpring())
            
                .frame(width: 300, height: 300)
            Button("执行动画") {
                enale.toggle()
            }
            
                
            
        }
        .ignoresSafeArea()
    }
    
    //MARK: - 单独控制某个属性
    @State private var enableCorner = false
    @State private var enableColor = false
    @State private var enableOpacity = false
    
    var explict: some View {
        VStack(spacing: 50) {
            RoundedRectangle(cornerRadius: enableCorner ? 100.0 : 0.0)
                .fill(enableColor ? Color.red : Color.green)
                .opacity(enableOpacity ? 1.0 : 0.3)
                .frame(width: 300, height: 300)
            Button("执行动画") {
                withAnimation(.linear(duration: 2)){
                   enableOpacity.toggle()
                 }
                enableColor.toggle()
                enableCorner.toggle()
            }
            
        }
    }
    //MARK: - 动画的控制
    var control: some View {
        VStack(spacing: 50) {
            RoundedRectangle(cornerRadius: enale ? 100.0 : 0.0)
                .fill(enale ? Color.red : Color.green)
                .opacity(enale ? 1.0 : 0.3)
                .frame(width: 300, height: 300)
                .animation(.easeInOut(duration: 3)
                )
            Button("执行动画") {
                enale.toggle()
            }
        }
    }
    
    //MARK: - 动画的控制
    var complex: some View {
        VStack(spacing: 50) {
            RoundedRectangle(cornerRadius: enale ? 100.0 : 0.0)
                .fill(enale ? Color.red : Color.green)
                .opacity(enale ? 1.0 : 0.3)
                .frame(width: 300, height: 300)
                .animation(.linear(duration: 2)
                            .delay(3)
//                            .repeatCount(3)
//                            .repeatForever()
//                            .repeatForever(autoreverses: false)
                            .speed(4)
                )
            Button("执行动画") {
                enale.toggle()
            }
        }
    }
}

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