๐Ÿ–ผ๏ธVehicle

โฌ†๏ธ ้œ€่ฆ๏ผš

import SwiftUI

struct Vehicle: View {
    
    // view state
    @State private var playing = false
    
    // car body shape
    let b = Vehicle.CarBodyShape()    // Vehicle.CarBodyShape
    
    // animation
    let movingForever = Animation
        .linear(duration: 0.3)
        .repeatForever(autoreverses: true)
    
    // view body
    var body: some View {
        ZStack {
            // 1. car body (filled)
            b
                // ๐ŸŒ€ShapeStyle+LinearGradient
                .fill(.horizontal(.green, .blue, .indigo))
                .aspectRatio(2, contentMode: .fit)
                .overlay {
                    
                    // 2. car body outline
                    b.stroke()
                    
                    // 3. wheels
                    let A: UnitPoint = [0.2, 1]    // ๐Ÿ…ฟ๏ธ Rectangular
                    let B: UnitPoint = [0.7, 1]
                    
                    Point(A, size: 60)             // ๐Ÿ–ผ Point
                    Point(A, .black, size: 30)
                    Point(B, size: 60)
                    Point(B, .black, size: 30)
                }
                .shadow(radius: 8)
            
                // animation
                .offset(y: playing ? -5 : 0)
                .onTapGesture {
                    withAnimation(movingForever) { 
                        self.playing.toggle()
                    }
                }
        }
    }
}

Last updated