Untitled

 avatar
unknown
plain_text
a year ago
3.9 kB
5
Indexable
ContentView:
import SwiftUI

struct ContentView: View {
    @State private var selectedShapeIndex: Int = 0
    @State private var fillColor = Color.blue
    @State private var widthText = ""
    @State private var heightText = ""
    @State private var showBorder = false
    
    var body: some View {
        VStack {
            VStack {
                HStack{
                    ShapePickerView(selectedShapeIndex: $selectedShapeIndex, options: ShapeData.options)
                    Spacer()
                    ColorPicker("", selection: $fillColor)
                    Spacer()
                }
                HStack{
                    Text("Szerokość: ")
                    TextField("Wprowadź szerokość", text: $widthText)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                }
                .padding()
                .padding(.bottom, -20)
                HStack{
                    Text("Wysokość: ")
                    TextField("Wprowadź wysokość", text: $heightText)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                }
                .padding()
                Toggle("Wyświetl ramkę", isOn: $showBorder)
                    .padding()

            }
            .border(Color.black)
            .padding()
            
            
            ShapeDrawingView(shapeOption: ShapeData.options[selectedShapeIndex], fillColor: fillColor, width: ShapeData.stringToCGFloat(widthText), height: ShapeData.stringToCGFloat(heightText), showBorder: showBorder)
        }
    }
}


#Preview {
    ContentView()
}


ShapeDrawingView:

import SwiftUI

struct ShapeDrawingView: View {
    let shapeOption: ShapeOption
    let fillColor: Color
    let width: CGFloat
    let height: CGFloat
    let showBorder: Bool
    
    var body: some View {
        ZStack {
            if showBorder {
                Rectangle()
                    .fill(Color.white)
                    .stroke(Color.black, lineWidth: 3)
                    .frame(width: shapeOption.name == "Kolo" ? min(width, height) : width, height: shapeOption.name == "Kolo" ? min(width, height) : height)
            }
            
            if shapeOption.name == "Kolo" {
                Circle()
                    .fill(fillColor)
                    .frame(width: min(width, height), height: min(width, height))
            } else if shapeOption.name == "Elipsa" {
                Ellipse()
                    .fill(fillColor)
                    .frame(width: width, height: height)
            } else if shapeOption.name == "Kwadrat" {
                Rectangle()
                    .fill(fillColor)
                    .frame(width: width, height: height)
            }
        }
    }
}


ShapePickerView:


import SwiftUI

struct ShapePickerView: View {
    @Binding var selectedShapeIndex: Int
    var options: [ShapeOption]
    
    var body: some View {
        Picker("Select a shape", selection: $selectedShapeIndex) {
            ForEach(options.indices, id: \.self) { index in
                HStack {
                    Text(options[index].name)
                    Image(systemName: options[index].iconName)
                }
                .tag(index)
            }
        }
      
    }
}


ShapeModel 


import Foundation

struct ShapeOption {
    let name: String
    let iconName: String
    let id: Int
}

struct ShapeData {
    static let options: [ShapeOption] = [
        ShapeOption(name: "Kolo", iconName: "circle", id: 0),
        ShapeOption(name: "Elipsa", iconName: "oval", id: 1),
        ShapeOption(name: "Kwadrat", iconName: "square", id: 2)
    ]
    
    static func stringToCGFloat(_ value: String) -> CGFloat {
        if let floatValue = Float(value) {
            return CGFloat(floatValue)
        } else {
            return 0
        }
    }
}

Editor is loading...
Leave a Comment