-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrashing Button.swift
183 lines (150 loc) · 5.99 KB
/
Crashing Button.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Just copy and paste the code into a blank playground template
// in Swift Playgrounds app on an iPad or a Mac
// Add an audiofile: press "plus" symbol on the right and choose a file
// !!! Turn off Enable Results in the settings, otherwise you'll get an error !!!
// Created by Roman Gaditskiy: https://GitHub.com/gadirom/Art-in-Swift
import SwiftUI
import PlaygroundSupport
import AVFoundation
let words = ["Satisfy!", "Yeah!", "Come on!"]
var points: [CGPoint] = [
CGPoint(x: 0, y: 0),
CGPoint(x: 1, y: 0),
CGPoint(x: 1, y: 1),
CGPoint(x: 0, y: 1)
]
struct CrashingButton: View {
var label: String
@State private var cracks: [Triangle] = []
@State private var d: CGFloat = 0
@State private var tapped = false
var body: some View{
ZStack{
let a = .pi / CGFloat(cracks.count) * 2
if !tapped{
Buttons(label: label, tapped: $tapped, crack: TriangleShape(points: points))
.frame(width: 100, height: 20)
}else{
ForEach(cracks.indices, id:\.self){id in
ZStack{
Buttons(label: label, tapped: $tapped, crack: TriangleShape(points: cracks[id]))
.frame(width: 200, height: 30)
}.offset(x: d*cos(CGFloat(id+1)*a), y: d*sin(CGFloat(id+1)*a))
}
}
}.onAppear(){
cracks = createCracks(n: Int.random(in: 4...5))
}
.onChange(of: tapped){_ in withAnimation(.easeOut(duration: 1), {d = 5})}
}
}
struct TriangleShape: Shape {
let points: [CGPoint]
func path(in rect: CGRect) -> Path {
Path { path in
let p = points.map(){CGPoint(x: $0.x * rect.width, y: $0.y * rect.height)}
path.addLines( p )
path.closeSubpath()
}
}
}
struct Buttons: View {
init(label: String, tapped: Binding<Bool>, crack: TriangleShape){
self.label = label
self._tapped = tapped
self.crack = crack
}
var label : String
var crack : TriangleShape
@Binding var tapped: Bool
@State var pressed: Bool = false
var body: some View {
VStack{
Text(label)
.foregroundColor(Color.black)
.font(.system(size: 20, weight: .semibold, design: .rounded))
.blur(radius: 0.4)
.frame(width:200, height: 60)
.background(
ZStack{
Color(#colorLiteral(red: 0.8407290577888489, green: 0.8310598731040955, blue: 0.9999999403953552, alpha: 1.0))
RoundedRectangle(cornerRadius: 30, style: .continuous)
.foregroundColor(.white)
.blur(radius: 10)
.offset(x: -8, y: -8)
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(
LinearGradient(gradient: Gradient(colors: [Color(#colorLiteral(red: 0.8293229937553406, green: 0.8711527585983276, blue: 1.000000238418579, alpha: 1.0)),Color.white]), startPoint: .topLeading, endPoint: .bottomTrailing)
)
.padding(2)
.blur(radius: 4)
}
)
.clipShape(crack)
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.shadow(color: Color(#colorLiteral(red: 0.6431992650032043, green: 0.6481773853302002, blue: 1.0000001192092896, alpha: 1.0)), radius: pressed ? 5 : 20,
x: pressed ? 3 : 20, y: pressed ? 3 : 20)
.shadow(color: Color(#colorLiteral(red: 0.9999160170555115, green: 1.0000032186508179, blue: 0.9998849034309387, alpha: 1.0)), radius: pressed ? 5 : 20,
x: pressed ? -5 : -20, y: pressed ? -5 : -20)
.scaleEffect(pressed ? 0.9 : 1)
.onTapGesture(count: 1){
withAnimation(.easeInOut(duration: 1.2)){pressed = true}
DispatchQueue.main.asyncAfter(deadline: .now() + 1){tapped = true; playSound()}
}
.onAppear(){ pressed = tapped}
}
}
}
typealias Triangle = [CGPoint]
func createCracks(n: Int) -> [Triangle]{
var a: [Double] = Array(repeating: 1, count: n)
a = a.map{ _ in Double.random(in: 1...10)}
let sum = a.reduce(0) {$0 + $1}
a = a.map{ $0 / sum * .pi * 2}
let c = CGPoint(x: 0.5, y: 0.5)
var a0: Double = a.last!
var triangles: [Triangle] = []
for a1 in a {
let t = [c,
rotate(a0)+c,
rotate(a0 + a1)+c]
a0 += a1
triangles.append(t)
}
return triangles
}
func rotate(_ a: Double) -> CGPoint{
return CGPoint(x: cos(a) * 2, y: sin(a) * 2)
}
extension CGPoint{
static func +(lhs: CGPoint, rhs: CGPoint) -> CGPoint{
CGPoint(
x: lhs.x + rhs.x,
y: lhs.y + rhs.y
)
}
}
var audioPlayer: AVAudioPlayer?
func playSound() {
do {
let audioFile = #fileLiteral(resourceName: "Wine-Glass-Shatering-A1-www.fesliyanstudios.com.mp3") // << --- Add a sound here!
audioPlayer = try AVAudioPlayer(contentsOf: audioFile)
audioPlayer?.play()
} catch {
print("ERROR: Can't initialize audioPlayer")
}
}
struct ContentView: View {
var body: some View{
ZStack{
Color(#colorLiteral(red: 0.8903511166572571, green: 0.8643324971199036, blue: 1.0000003576278687, alpha: 1.0))
VStack{
ForEach(words.indices, id: \.self){id in
CrashingButton(label: words[id]).frame(width: 300, height: 100)
}
}
}.frame(maxWidth:.infinity, maxHeight: .infinity)
.ignoresSafeArea(.all)
}
}
PlaygroundPage.current.setLiveView(ContentView())