-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// CanvasView.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct CanvasView: View { | ||
|
||
@Binding var lines: [Line] | ||
@Binding var selectedColor: Color | ||
@Binding var selectedLineWidth: CGFloat | ||
|
||
var body: some View { | ||
Canvas { context, size in | ||
for line in lines { | ||
|
||
var path = Path() | ||
path.addLines(line.points) | ||
|
||
context.stroke(path, with: .color(line.color), lineWidth: line.lineWidth) | ||
} | ||
}.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({ value in | ||
let newLocation = value.location | ||
|
||
if value.translation.width + value.translation.height == 0 { | ||
lines.append(Line(points: [newLocation], | ||
color: selectedColor, | ||
lineWidth: selectedLineWidth)) | ||
} else { | ||
let index = lines.count - 1 | ||
lines[index].points.append(newLocation) | ||
|
||
} | ||
})) | ||
} | ||
} | ||
|
||
struct CanvasView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
CanvasView(lines: .constant([Line(points: [CGPoint(x: 1, y: 1)], | ||
color: .black, lineWidth: 1)]), | ||
selectedColor: .constant(.black), | ||
selectedLineWidth: .constant(1)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// ColorPicker.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ColorPickerView: View { | ||
|
||
@Binding var selectedColor: Color | ||
var body: some View { | ||
ColorPicker ("Color", selection: $selectedColor) | ||
.labelsHidden() | ||
} | ||
} | ||
|
||
struct ColorPicker_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ColorPickerView(selectedColor: .constant(.black)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// SliderView.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct SliderView: View { | ||
|
||
@Binding var selectedLineWidth: CGFloat | ||
var body: some View { | ||
Slider(value: $selectedLineWidth, | ||
in: 1...10) {} | ||
} | ||
} | ||
|
||
struct SliderView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
SliderView(selectedLineWidth: .constant(1)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// DrawingView.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct DrawingView: View { | ||
|
||
@State private var lines = [Line]() | ||
@State private var selectedColor: Color = .black | ||
@State private var selectedLineWidth: CGFloat = 1 | ||
|
||
var body: some View { | ||
GeometryReader { geo in | ||
VStack { | ||
CanvasView(lines: $lines, | ||
selectedColor: $selectedColor, | ||
selectedLineWidth: $selectedLineWidth) | ||
HStack { | ||
ColorPickerView(selectedColor: $selectedColor) | ||
SliderView(selectedLineWidth: $selectedLineWidth) | ||
.frame(width: geo.size.width / 2) | ||
Text(String(format: "%.f", selectedLineWidth)) | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct DrawingView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
DrawingView() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Line.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct Line { | ||
|
||
var points: [CGPoint] | ||
var color: Color | ||
var lineWidth: CGFloat | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// ScreenSizes.swift | ||
// SwiftUICanvas | ||
// | ||
// Created by Egemen TÜRK on 28.01.2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension DrawingView { | ||
static let screenWidth = UIScreen.main.bounds.size.width | ||
static let screenHeight = UIScreen.main.bounds.size.height | ||
static let screenSize = UIScreen.main.bounds.size | ||
} |