Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 24, 2024 01:37
Show Gist options
  • Select an option

  • Save takoikatakotako/8e48fbe489b2ec074a325942ec51e244 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/8e48fbe489b2ec074a325942ec51e244 to your computer and use it in GitHub Desktop.
FunctionBuilderを使ってViewに影をつける
import SwiftUI
struct CardView<Content>: View where Content: View {
let color: Color
let radius: CGFloat
let content: () -> Content
init(
color: Color = Color.gray.opacity(0.4),
radius: CGFloat = 8,
@ViewBuilder content: @escaping () -> Content) {
self.content = content
self.color = color
self.radius = radius
}
var body: some View {
content()
.padding(16)
.background(Color.white)
.cornerRadius(16)
.shadow(color: color, radius: radius, x: 4, y: 4)
.padding(radius + 8)
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
CardView {
Text("Snorlax")
}
CardView {
Image(.icon)
.resizable()
.frame(width: 60, height: 60)
}
CardView(color: Color.red.opacity(0.4)) {
Text("RedShadow")
}
CardView(
color: Color.green.opacity(0.4),
radius: 24) {
Text("BigShadow")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment