Skip to content

Commit

Permalink
Add randomize function
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 27, 2022
1 parent de56ad7 commit e0d5953
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
27 changes: 17 additions & 10 deletions Example/PrismExample/PrismExample/Showcase/Slime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,27 @@ struct SlimeDetailView: View {
]

var body: some View {
PrismCanvas(tilt: 0.3) {
HStack(spacing: 20) {
ForEach($displayedConfigurations) { $displayedConfiguration in
let configuration = Binding {
$displayedConfiguration.wrappedValue.configuration
} set: { newValue in
$displayedConfiguration.wrappedValue.configuration = newValue
}
VStack {
Text("Tap me!")
.font(.title)
.foregroundColor(UIColor.secondaryLabel.color)
.offset(y: -200)

PrismCanvas(tilt: 0.3) {
HStack(spacing: 20) {
ForEach($displayedConfigurations) { $displayedConfiguration in
let configuration = Binding {
$displayedConfiguration.wrappedValue.configuration
} set: { newValue in
$displayedConfiguration.wrappedValue.configuration = newValue
}

SlimePrismView(configuration: configuration)
SlimePrismView(configuration: configuration)
}
}
}
}
.navigationTitle("Color")
.navigationTitle("Slime")
}
}

Expand Down
24 changes: 23 additions & 1 deletion Example/PrismExample/PrismExample/Templates/DetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ struct DetailView<Content: View, Controls: View>: View {
.fontWeight(.semibold)
Divider()
sliders

GridRow {
Button("Randomize") {
randomize()
}
.buttonStyle(.borderedProminent)

Color.clear
.gridCellUnsizedAxes(.horizontal)
}
}
}
.padding(.vertical, 16)
Expand All @@ -69,9 +79,21 @@ struct DetailView<Content: View, Controls: View>: View {
ExampleSliderView(title: "Width", value: width, range: 0 ... 200)
ExampleSliderView(title: "Height", value: height, range: 0 ... 200)
ExampleSliderView(title: "Extrusion", value: $configuration.extrusion, range: 0 ... 100)
ExampleSliderView(title: "Levitation", value: $configuration.levitation, range: 0 ... 200)
ExampleSliderView(title: "Levitation", value: $configuration.levitation, range: 0 ... 100)
ExampleColorView(title: "Shadow Color", value: $configuration.shadowColor)
ExampleSliderView(title: "Shadow Opacity", value: $configuration.shadowOpacity, range: 0 ... 1)
}
}

func randomize() {
withAnimation(.spring()) {
configuration.tilt = CGFloat.random(in: 0 ..< 1)
configuration.size.height = CGFloat.random(in: 0 ..< 200)
configuration.size.width = CGFloat.random(in: 0 ..< 200)
configuration.extrusion = CGFloat.random(in: 0 ..< 100)
configuration.levitation = CGFloat.random(in: 0 ..< 100)
configuration.shadowColor = UIColor.random.color
configuration.shadowOpacity = CGFloat.random(in: 0 ..< 1)
}
}
}
11 changes: 11 additions & 0 deletions Example/PrismExample/PrismExample/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ struct PressedButtonStyle: ButtonStyle {
.opacity(1) /// needs a modifier to actually be called
}
}

extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0 ... 1),
green: .random(in: 0 ... 1),
blue: .random(in: 0 ... 1),
alpha: 1.0
)
}
}

0 comments on commit e0d5953

Please sign in to comment.