# Gradient + View

[swift](https://lochiwei.gitbook.io/ios/swift)⟩ [custom](https://lochiwei.gitbook.io/ios/custom) ⟩ [extension](https://lochiwei.gitbook.io/ios/custom/ext) ⟩ [Gradient](https://lochiwei.gitbook.io/ios/custom/ext/gradient) ⟩ + View

{% hint style="info" %}
Let [`Gradient`](https://lochiwei.gitbook.io/ios/swiftui/shapes/shapestyle/gradient) and [<mark style="color:blue;">`AnyGradient`</mark>](https://developer.apple.com/documentation/swiftui/anygradient) comform to [`View`](https://lochiwei.gitbook.io/ios/swiftui/view/view).
{% endhint %}

<figure><img src="https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2F4VZaqXrvRi58byF1Rhai%2FAnyGradient%20as%20View.jpeg?alt=media&#x26;token=bde2b483-c67e-48cf-9bef-9cbe002b92fb" alt=""><figcaption><p>原始碼： <span data-gb-custom-inline data-tag="emoji" data-code="1f449">👉</span> 「💾 程式」</p></figcaption></figure>

{% tabs %}
{% tab title="💾 程式" %}

```swift
import SwiftUI

// Gradient + View
// now `Gradient` is a view.
extension Gradient: View {
    public var body: some View {
        Rectangle().fill(self)
    }
}

// AnyGradient + View
// now `AnyGradient` is a view.
extension AnyGradient: View {
    public var body: some View {
        Rectangle().fill(self)
    }
}
```

📁 Previews

```swift
// previews
struct GradientViewExample_Previews: PreviewProvider {
    
    // first 11 standard colors
    static let colors = Array(Color.standardColors.prefix(11))
    
    static var previews: some View {
        HStack {
            ForEach(colors, id: \.self){color in
                Swatch(name: color.name, size: 60){   // 🖼️ custom view
                    color.gradient                    // 🌀 AnyGradient + View
                }  
            }
        }
    }// previews
}
```

{% endtab %}

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

* SwiftUI ⟩ [Drawing and Animation](https://developer.apple.com/documentation/swiftui/drawing-and-animation) ⟩ [LinearGradient](https://developer.apple.com/documentation/swiftui/lineargradient)
* [Gradient](https://developer.apple.com/documentation/swiftui/gradient)
* [AnyGradient](https://developer.apple.com/documentation/swiftui/anygradient)
  {% endtab %}

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

* [gradient](https://lochiwei.gitbook.io/ios/swiftui/shapes/shapestyle/gradient "mention")
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: 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:

```
GET https://lochiwei.gitbook.io/ios/custom/ext/gradient/lineargradient-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
