-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentView.swift
94 lines (74 loc) · 2.58 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// ContentView.swift
// P09X Drawing
//
// Created by Julian Moorhouse on 08/03/2021.
//
import SwiftUI
struct Arrow: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
let quarterX = (rect.midX - rect.minX) / 2
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.midY))
path.addLine(to: CGPoint(x: rect.minX + quarterX, y: rect.midY))
path.addLine(to: CGPoint(x: rect.minX + quarterX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.midX + quarterX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.midX + quarterX, y: rect.midY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.midY))
path.addLine(to: CGPoint(x: rect.midX, y: rect.minY))
return path
}
}
struct ColorCyclingRect: View {
var amount = 0.0
var steps = 100
var body: some View {
ZStack {
ForEach(0..<steps) { value in
Rectangle()
.inset(by: CGFloat(value))
.strokeBorder(self.color(for: value, brightness: 1), lineWidth: 1)
}
}
}
func color(for value: Int, brightness: Double) -> Color {
var targetHue = Double(value) / Double(self.steps) + self.amount
if targetHue > 1 {
targetHue -= 1
}
return Color(hue: targetHue, saturation: 1, brightness: brightness)
}
}
struct ContentView: View {
@State private var lineWidth: CGFloat = 10.0
@State private var colorCycle = 0.5
var body: some View {
VStack(spacing: 30) {
Spacer()
Group {
Arrow()
.stroke(Color.red, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
.onTapGesture {
withAnimation(.linear(duration: 1)) {
self.lineWidth = self.lineWidth == 10.0 ? 20.00 : 10.0
}
}
.frame(width: 300)
Text("Tap arrow")
}
Group {
ColorCyclingRect(amount: self.colorCycle)
.frame(width: 300)
Text("Cycle colour: \(colorCycle)")
Slider(value: $colorCycle)
.padding([.horizontal, .bottom])
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}