Created
October 24, 2024 01:35
-
-
Save takoikatakotako/16643bf6719b95ee71971319f202c2ed to your computer and use it in GitHub Desktop.
ViewModifierを使ってViewに影をつける
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct CardViewModifier: ViewModifier { | |
| let color: Color | |
| let radius: CGFloat | |
| func body(content: Content) -> some View { | |
| content | |
| .padding(16) | |
| .background(Color.white) | |
| .cornerRadius(16) | |
| .shadow(color: color, radius: radius, x: 4, y: 4) | |
| .padding(radius + 8) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ContentView: View { | |
| var body: some View { | |
| VStack { | |
| Text("Snorlax") | |
| .card() | |
| Image(.icon) | |
| .resizable() | |
| .frame(width: 60, height: 60) | |
| .card() | |
| Text("RedShadow") | |
| .card(color: Color.red.opacity(0.4)) | |
| Text("BigShadow") | |
| .card( | |
| color: Color.green.opacity(0.4), | |
| radius: 24) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| extension View { | |
| func card( | |
| color: Color = Color.gray.opacity(0.4), | |
| radius: CGFloat = 8) -> some View { | |
| self.modifier(CardViewModifier(color: color, radius: radius)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment