Last updated 2 years ago
SwiftUI โฉ Views โฉ View Modifiers โฉ examples โฉ
NSFW ไปฃ่กจใNot Safe For Workใ๏ผใไธ็ญไธๅฎใ็ๆๆใ้ๅไพๅญๅฏไปฅ็จใ่งธๆงใไพๆงๅถ่ฆไธ่ฆๅฐๅ็ๆจก็ณๅใ
struct ContentView: View { var body: some View { HStack { tiger Color.orange.aspectRatio(1, contentMode: .fit) }.frame(height: 300) } var tiger: some View { // ไนๅฏ็จ Image(playground: "...") Image(uiImage: #imageLiteral(resourceName: "tiger.heic")) .resizable() .scaledToFit() .shadow(radius: 4) .nsfw() // โญ๏ธ view modifier: ๐ .nsfw() .padding() .background(Color.gray) } }
import SwiftUI // (internal) view modifier struct NSFW: ViewModifier { // โญ๏ธ ่ฒ ่ฒฌ่ฆไธ่ฆๅฅ็จใๆจก็ณใๆๆ @State private var blur = true // body func body(content: Content) -> some View { content .blur(radius: blur ? 20 : 0) // โญ๏ธ ๆจก็ณๆๆ .clipped() // โญ๏ธ ไธ่ฎๆจก็ณๆๆๆดๆฃๅบๅป .overlay(nsfwText) // โญ๏ธ ๅ็ไธๆๅญ // โญ๏ธ ๅฉ็จ่งธๆงๆงๅถใๆจก็ณใๆๆ .onTapGesture { withAnimation { // โญ๏ธ ๅ็จใๅ็ซใๆๆ self.blur.toggle() // โญ๏ธ ๅๆ `blur` } } } // overlay text var nsfwText: some View { VStack { Image(systemName: "eye.slash.fill") Text("NSFW").font(.caption) } .foregroundColor(.white) .opacity(blur ? 1 : 0) // โญ๏ธ ้กฏ็คบๆ้ฑ่ๆๅญ } } // view modifier helper extension View { public func nsfw() -> some View { modifier(NSFW()) } }
Sarun โฉ SwiftUI's ViewModifier
SwiftUI โฉ View โฉ .modifier(_:)
Image(playground:) - Image extension