Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
egementrk committed Jan 28, 2023
1 parent 5f134d8 commit 2f96201
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 7 deletions.
28 changes: 28 additions & 0 deletions SwiftUICanvas.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
885AFA9C298501DA00B5A562 /* SwiftUICanvasTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFA9B298501DA00B5A562 /* SwiftUICanvasTests.swift */; };
885AFAA6298501DA00B5A562 /* SwiftUICanvasUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFAA5298501DA00B5A562 /* SwiftUICanvasUITests.swift */; };
885AFAA8298501DA00B5A562 /* SwiftUICanvasUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFAA7298501DA00B5A562 /* SwiftUICanvasUITestsLaunchTests.swift */; };
885AFAB52985020400B5A562 /* DrawingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFAB42985020400B5A562 /* DrawingView.swift */; };
885AFAB72985093F00B5A562 /* Line.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFAB62985093F00B5A562 /* Line.swift */; };
885AFABB298513F900B5A562 /* ColorPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 885AFABA298513F900B5A562 /* ColorPickerView.swift */; };
889D108629851B6F00F6AB87 /* SliderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 889D108529851B6F00F6AB87 /* SliderView.swift */; };
889D108829851C7D00F6AB87 /* CanvasView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 889D108729851C7D00F6AB87 /* CanvasView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -44,6 +49,11 @@
885AFAA1298501DA00B5A562 /* SwiftUICanvasUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftUICanvasUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
885AFAA5298501DA00B5A562 /* SwiftUICanvasUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUICanvasUITests.swift; sourceTree = "<group>"; };
885AFAA7298501DA00B5A562 /* SwiftUICanvasUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUICanvasUITestsLaunchTests.swift; sourceTree = "<group>"; };
885AFAB42985020400B5A562 /* DrawingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawingView.swift; sourceTree = "<group>"; };
885AFAB62985093F00B5A562 /* Line.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Line.swift; sourceTree = "<group>"; };
885AFABA298513F900B5A562 /* ColorPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPickerView.swift; sourceTree = "<group>"; };
889D108529851B6F00F6AB87 /* SliderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SliderView.swift; sourceTree = "<group>"; };
889D108729851C7D00F6AB87 /* CanvasView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CanvasView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -94,8 +104,11 @@
885AFA89298501D700B5A562 /* SwiftUICanvas */ = {
isa = PBXGroup;
children = (
889D108429851B5E00F6AB87 /* CustomViews */,
885AFA8A298501D700B5A562 /* SwiftUICanvasApp.swift */,
885AFA8C298501D700B5A562 /* ContentView.swift */,
885AFAB42985020400B5A562 /* DrawingView.swift */,
885AFAB62985093F00B5A562 /* Line.swift */,
885AFA8E298501D900B5A562 /* Assets.xcassets */,
885AFA90298501D900B5A562 /* Preview Content */,
);
Expand Down Expand Up @@ -127,6 +140,16 @@
path = SwiftUICanvasUITests;
sourceTree = "<group>";
};
889D108429851B5E00F6AB87 /* CustomViews */ = {
isa = PBXGroup;
children = (
885AFABA298513F900B5A562 /* ColorPickerView.swift */,
889D108529851B6F00F6AB87 /* SliderView.swift */,
889D108729851C7D00F6AB87 /* CanvasView.swift */,
);
path = CustomViews;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -257,6 +280,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
885AFAB52985020400B5A562 /* DrawingView.swift in Sources */,
889D108829851C7D00F6AB87 /* CanvasView.swift in Sources */,
885AFAB72985093F00B5A562 /* Line.swift in Sources */,
885AFABB298513F900B5A562 /* ColorPickerView.swift in Sources */,
889D108629851B6F00F6AB87 /* SliderView.swift in Sources */,
885AFA8D298501D700B5A562 /* ContentView.swift in Sources */,
885AFA8B298501D700B5A562 /* SwiftUICanvasApp.swift in Sources */,
);
Expand Down
8 changes: 1 addition & 7 deletions SwiftUICanvas/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ import SwiftUI

struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
DrawingView()
}
}

Expand Down
48 changes: 48 additions & 0 deletions SwiftUICanvas/CustomViews/CanvasView.swift
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))
}
}
23 changes: 23 additions & 0 deletions SwiftUICanvas/CustomViews/ColorPickerView.swift
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))
}
}
23 changes: 23 additions & 0 deletions SwiftUICanvas/CustomViews/SliderView.swift
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))
}
}
38 changes: 38 additions & 0 deletions SwiftUICanvas/DrawingView.swift
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()
}
}
15 changes: 15 additions & 0 deletions SwiftUICanvas/Line.swift
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
}
14 changes: 14 additions & 0 deletions SwiftUICanvas/ScreenSizes.swift
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
}

0 comments on commit 2f96201

Please sign in to comment.