-
-
Save kellylougheed/f8a86377abe5d35e4ae389285da85eab to your computer and use it in GitHub Desktop.
SwiftUI app for a paint canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// 2Darrays | |
// | |
// Created by Kelly Lougheed on 12/3/20. | |
// | |
import SwiftUI | |
var selectedColor: Color = Color.black | |
struct MyRectangle: View { | |
@State var myColor: Color | |
var body: some View { | |
Button(action: { | |
myColor = selectedColor | |
}) { | |
Rectangle() | |
.fill(myColor) | |
.aspectRatio(contentMode: .fit) | |
.border(Color.black, width: 0.5) | |
} | |
} | |
} | |
struct ColorButton: View { | |
@State var color: Color | |
var body: some View { | |
Button(action: { | |
selectedColor = color | |
}) { | |
Rectangle() | |
.fill(color) | |
.aspectRatio(contentMode: .fit) | |
} | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
VStack { | |
Text("Select your color...") | |
HStack { | |
ColorButton(color: Color.red) | |
ColorButton(color: Color.yellow) | |
ColorButton(color: Color.green) | |
ColorButton(color: Color.blue) | |
} | |
Text("...and draw on the canvas below!") | |
Button(action: { | |
selectedColor = Color.white | |
}) { | |
Text("Eraser").padding(20) | |
} | |
Spacer() | |
HStack(spacing: 0) { | |
ForEach(0..<10) { x in | |
VStack(spacing: 0) { | |
ForEach(0..<10) { y in | |
MyRectangle(myColor: Color.white) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment