Skip to content

Commit

Permalink
Updated samples
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaTesser committed Nov 8, 2024
1 parent 8950500 commit bb6dd38
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 43 deletions.
Binary file not shown.
6 changes: 6 additions & 0 deletions SwiftUIPlayground/Samples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,10 @@ public let samples: [String: AnyView] = [
"ViewModifier Protocol": AnyView(ViewModifierProtocolSample()),
]
)),
"Shapes": AnyView(SamplesList(
title: "Shapes", samples:
[
"Progress Indicator Arc": AnyView(ProgressIndicatorArcSample()),
]
)),
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct MatchedGeometryEffectSample: View {
@Namespace private var animation
@State private var isExpanded: Bool = false

var body: some View {
ZStack {
if isExpanded {
Expand All @@ -24,7 +24,6 @@ struct MatchedGeometryEffectSample: View {
.scaledToFit()
.padding()
.matchedGeometryEffect(id: "park", in: animation)

}
} else {
ZStack {
Expand All @@ -47,7 +46,8 @@ struct MatchedGeometryEffectSample: View {
isExpanded.toggle()
}
}
.ignoresSafeArea() }
.ignoresSafeArea()
}
}

#Preview {
Expand Down
40 changes: 19 additions & 21 deletions SwiftUIPlayground/Samples/CustomLayout/LayoutProtocolSample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,44 @@ struct LayoutProtocolSample: View {
}

struct BackslashStack: Layout {

func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {

func sizeThatFits(proposal _: ProposedViewSize, subviews: Subviews, cache _: inout ()) -> CGSize {
let subviewSizes = subviews.map { proxy in
return proxy.sizeThatFits(.unspecified)
proxy.sizeThatFits(.unspecified)
}

let combinedSize = subviewSizes.reduce(.zero) { currentSize, subviewSize in

return CGSize(
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height)

CGSize(
width: currentSize.width + subviewSize.width, height: currentSize.height + subviewSize.height
)
}

return combinedSize
}

func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {



func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache _: inout ()) {
let subviewSizes = subviews.map { proxy in
return proxy.sizeThatFits(.unspecified)
proxy.sizeThatFits(.unspecified)
}

var x = bounds.minX
var y = bounds.minY

for index in subviews.indices {
let subviewSize = subviewSizes[index]
let sizeProposal = ProposedViewSize(
width: subviewSize.width,
height: subviewSize.height)

height: subviewSize.height
)

subviews[index]
.place(
at: CGPoint(x: x, y: y),
proposal: sizeProposal)

proposal: sizeProposal
)

x += subviewSize.width
y += subviewSize.height
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct AlignmentGuideSample: View {

var body: some View {
VStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 20)
Expand All @@ -33,9 +32,7 @@ struct AlignmentGuideSample: View {
.fill(Color.green)
.frame(width: 50, height: 50)
}

}

}

#Preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ struct UploadProgressView: View {
.frame(width: 100)
Text("\(uploadProgress.formatted(.percent))")
}

}
}
56 changes: 56 additions & 0 deletions SwiftUIPlayground/Samples/Shapes/ProgressIndicatorArcSample.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ProgressIndicatorArcSample.swift
// SwiftUIPlayground
//
// Created by Taha Tesser on 11/5/24.
//

import SwiftUI

struct ProgressIndicatorArcSample: View {
let sunYellow = Color(UIColor(red: 1.0, green: 0.85, blue: 0.2, alpha: 1.0)) // RGB: (255, 217, 51)
let sunOrange = Color(UIColor(red: 1.0, green: 0.64, blue: 0.0, alpha: 1.0)) // RGB: (255, 165, 0)
var body: some View {
ZStack {
_ProgressView(
startAngle: .degrees(-90),
endAngle: .degrees(90),
clockwise: true
)
.fill(sunOrange)

_ProgressView(
startAngle: .degrees(-90),
endAngle: .degrees(90),
clockwise: false
)
.fill(sunYellow)
}

.padding()
}
}

struct _ProgressView: Shape {
var startAngle: Angle
var endAngle: Angle
var clockwise: Bool

func path(in rect: CGRect) -> Path {
var path = Path()

path.addArc(
center: CGPoint(x: rect.midX, y: rect.midY),
radius: rect.width / 2,
startAngle: startAngle,
endAngle: endAngle,
clockwise: clockwise
)

return path
}
}

#Preview {
ProgressIndicatorArcSample()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ViewModifierProtocolSamp,e.swift
// ViewModifierProtocolSample.swift
// SwiftUIPlayground
//
// Created by Taha Tesser on 10/14/24.
Expand All @@ -10,18 +10,18 @@ import SwiftUI
struct ViewModifierProtocolSample: View {
@State var text: String = "Hello, World!"


var body: some View {
TextField(
"",
text: $text,
prompt: Text("Placeholder"))
prompt: Text("Placeholder")
)
.clearButton(
text: $text,
onClearHandler: {
print("Text wad cleared!")

})
}
)
// .modifier(ClearTextButtonViewModifier(text: $text, onClearHandler: {
// print("Text wad cleared!")
// }))
Expand All @@ -34,29 +34,29 @@ struct ViewModifierProtocolSample: View {
}

extension View {

func clearButton(
text: Binding<String>,
onClearHandler: (() -> Void)?) -> some View {
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler))
}
onClearHandler: (() -> Void)?
) -> some View {
modifier(ClearTextButtonViewModifier(text: text, onClearHandler: onClearHandler))
}
}

private struct ClearTextButtonViewModifier: ViewModifier {
@Binding var text: String

let onClearHandler: (() -> Void)?

func body(content: Content) -> some View {
ZStack {
content

HStack {
Spacer()

Button {
text.removeAll()

onClearHandler?()
} label: {
Image(systemName: "xmark.circle.fill")
Expand All @@ -69,4 +69,3 @@ private struct ClearTextButtonViewModifier: ViewModifier {
}
}
}

0 comments on commit bb6dd38

Please sign in to comment.