Skip to content

Commit bb6dd38

Browse files
committed
Updated samples
1 parent 8950500 commit bb6dd38

File tree

8 files changed

+98
-43
lines changed

8 files changed

+98
-43
lines changed

SwiftUIPlayground/Samples.swift

+6
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public let samples: [String: AnyView] = [
168168
"ViewModifier Protocol": AnyView(ViewModifierProtocolSample()),
169169
]
170170
)),
171+
"Shapes": AnyView(SamplesList(
172+
title: "Shapes", samples:
173+
[
174+
"Progress Indicator Arc": AnyView(ProgressIndicatorArcSample()),
175+
]
176+
)),
171177
]

SwiftUIPlayground/Samples/Animations/MatchedGeometryEffectSample.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010
struct MatchedGeometryEffectSample: View {
1111
@Namespace private var animation
1212
@State private var isExpanded: Bool = false
13-
13+
1414
var body: some View {
1515
ZStack {
1616
if isExpanded {
@@ -24,7 +24,6 @@ struct MatchedGeometryEffectSample: View {
2424
.scaledToFit()
2525
.padding()
2626
.matchedGeometryEffect(id: "park", in: animation)
27-
2827
}
2928
} else {
3029
ZStack {
@@ -47,7 +46,8 @@ struct MatchedGeometryEffectSample: View {
4746
isExpanded.toggle()
4847
}
4948
}
50-
.ignoresSafeArea() }
49+
.ignoresSafeArea()
50+
}
5151
}
5252

5353
#Preview {

SwiftUIPlayground/Samples/CustomLayout/LayoutProtocolSample.swift

+19-21
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,44 @@ struct LayoutProtocolSample: View {
2828
}
2929

3030
struct BackslashStack: Layout {
31-
32-
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
33-
31+
func sizeThatFits(proposal _: ProposedViewSize, subviews: Subviews, cache _: inout ()) -> CGSize {
3432
let subviewSizes = subviews.map { proxy in
35-
return proxy.sizeThatFits(.unspecified)
33+
proxy.sizeThatFits(.unspecified)
3634
}
37-
35+
3836
let combinedSize = subviewSizes.reduce(.zero) { currentSize, subviewSize in
39-
40-
return CGSize(
41-
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height)
37+
38+
CGSize(
39+
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height
40+
)
4241
}
43-
42+
4443
return combinedSize
4544
}
46-
47-
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
48-
49-
45+
46+
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache _: inout ()) {
5047
let subviewSizes = subviews.map { proxy in
51-
return proxy.sizeThatFits(.unspecified)
48+
proxy.sizeThatFits(.unspecified)
5249
}
53-
50+
5451
var x = bounds.minX
5552
var y = bounds.minY
56-
53+
5754
for index in subviews.indices {
5855
let subviewSize = subviewSizes[index]
5956
let sizeProposal = ProposedViewSize(
6057
width: subviewSize.width,
61-
height: subviewSize.height)
62-
58+
height: subviewSize.height
59+
)
60+
6361
subviews[index]
6462
.place(
6563
at: CGPoint(x: x, y: y),
66-
proposal: sizeProposal)
67-
64+
proposal: sizeProposal
65+
)
66+
6867
x += subviewSize.width
6968
y += subviewSize.height
7069
}
7170
}
72-
7371
}

SwiftUIPlayground/Samples/LayoutAdjustments/AlignmentGuideSample.swift

-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import SwiftUI
99

1010
struct AlignmentGuideSample: View {
11-
1211
var body: some View {
1312
VStack(alignment: .leading) {
1413
RoundedRectangle(cornerRadius: 20)
@@ -33,9 +32,7 @@ struct AlignmentGuideSample: View {
3332
.fill(Color.green)
3433
.frame(width: 50, height: 50)
3534
}
36-
3735
}
38-
3936
}
4037

4138
#Preview {

SwiftUIPlayground/Samples/LayoutFundamentals/ViewThatFitsSample.swift

-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ struct UploadProgressView: View {
3838
.frame(width: 100)
3939
Text("\(uploadProgress.formatted(.percent))")
4040
}
41-
4241
}
4342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// ProgressIndicatorArcSample.swift
3+
// SwiftUIPlayground
4+
//
5+
// Created by Taha Tesser on 11/5/24.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ProgressIndicatorArcSample: View {
11+
let sunYellow = Color(UIColor(red: 1.0, green: 0.85, blue: 0.2, alpha: 1.0)) // RGB: (255, 217, 51)
12+
let sunOrange = Color(UIColor(red: 1.0, green: 0.64, blue: 0.0, alpha: 1.0)) // RGB: (255, 165, 0)
13+
var body: some View {
14+
ZStack {
15+
_ProgressView(
16+
startAngle: .degrees(-90),
17+
endAngle: .degrees(90),
18+
clockwise: true
19+
)
20+
.fill(sunOrange)
21+
22+
_ProgressView(
23+
startAngle: .degrees(-90),
24+
endAngle: .degrees(90),
25+
clockwise: false
26+
)
27+
.fill(sunYellow)
28+
}
29+
30+
.padding()
31+
}
32+
}
33+
34+
struct _ProgressView: Shape {
35+
var startAngle: Angle
36+
var endAngle: Angle
37+
var clockwise: Bool
38+
39+
func path(in rect: CGRect) -> Path {
40+
var path = Path()
41+
42+
path.addArc(
43+
center: CGPoint(x: rect.midX, y: rect.midY),
44+
radius: rect.width / 2,
45+
startAngle: startAngle,
46+
endAngle: endAngle,
47+
clockwise: clockwise
48+
)
49+
50+
return path
51+
}
52+
}
53+
54+
#Preview {
55+
ProgressIndicatorArcSample()
56+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ViewModifierProtocolSamp,e.swift
2+
// ViewModifierProtocolSample.swift
33
// SwiftUIPlayground
44
//
55
// Created by Taha Tesser on 10/14/24.
@@ -10,18 +10,18 @@ import SwiftUI
1010
struct ViewModifierProtocolSample: View {
1111
@State var text: String = "Hello, World!"
1212

13-
1413
var body: some View {
1514
TextField(
1615
"",
1716
text: $text,
18-
prompt: Text("Placeholder"))
17+
prompt: Text("Placeholder")
18+
)
1919
.clearButton(
2020
text: $text,
2121
onClearHandler: {
2222
print("Text wad cleared!")
23-
24-
})
23+
}
24+
)
2525
// .modifier(ClearTextButtonViewModifier(text: $text, onClearHandler: {
2626
// print("Text wad cleared!")
2727
// }))
@@ -34,29 +34,29 @@ struct ViewModifierProtocolSample: View {
3434
}
3535

3636
extension View {
37-
3837
func clearButton(
3938
text: Binding<String>,
40-
onClearHandler: (() -> Void)?) -> some View {
41-
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler))
42-
}
39+
onClearHandler: (() -> Void)?
40+
) -> some View {
41+
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler))
42+
}
4343
}
4444

4545
private struct ClearTextButtonViewModifier: ViewModifier {
4646
@Binding var text: String
47-
47+
4848
let onClearHandler: (() -> Void)?
49-
49+
5050
func body(content: Content) -> some View {
5151
ZStack {
5252
content
53-
53+
5454
HStack {
5555
Spacer()
56-
56+
5757
Button {
5858
text.removeAll()
59-
59+
6060
onClearHandler?()
6161
} label: {
6262
Image(systemName: "xmark.circle.fill")
@@ -69,4 +69,3 @@ private struct ClearTextButtonViewModifier: ViewModifier {
6969
}
7070
}
7171
}
72-

0 commit comments

Comments
 (0)