> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/view/modifier/examples/nsfw.md).

# .nsfw()

[SwiftUI](/ios/swiftui.md) ⟩ [Views](/ios/swiftui/view.md) ⟩ [View Modifiers](/ios/swiftui/view/modifier/viewmodifier.md) ⟩ [examples](/ios/swiftui/view/modifier/examples.md) ⟩

{% tabs %}
{% tab title="💈範例" %}
{% hint style="info" %}
**NSFW** 代表「**N**ot **S**afe **F**or **W**ork」，「上班不宜」的意思。這個例子可以用「觸控」來控制要不要將圖片模糊化。
{% endhint %}

{% embed url="<https://youtu.be/zlQJdlHEWnQ>" %}

```swift
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)
    }
}
```

{% endtab %}

{% tab title="🌀 .nsfw()" %}

```swift
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()) }
}
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] Sarun ⟩ [SwiftUI's ViewModifier](https://sarunw.com/posts/swiftui-viewmodifier/)&#x20;
  {% endtab %}

{% tab title="📘 手冊" %}

* SwiftUI ⟩ View ⟩ .[**modifier**](https://developer.apple.com/documentation/swiftui/view/modifier\(_:\))(\_:)&#x20;
  {% endtab %}

{% tab title="👥 相關" %}

* [Image(playground:)](/ios/custom/ext/image/image-playground.md) - Image extension
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/view/modifier/examples/nsfw.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
