> 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/custom/ext/gradient/lineargradient-1.md).

# Gradient + View

╱🚧 under construction -> LinearGradient , Array.first(), Swatch

[swift](/ios/swift.md)⟩ [custom](/ios/custom.md) ⟩ [extension](/ios/custom/ext.md) ⟩ [Gradient](/ios/custom/ext/gradient.md) ⟩ + View

{% hint style="info" %}
Let [`Gradient`](/ios/swiftui/shapes/shapestyle/gradient.md) and [<mark style="color:blue;">`AnyGradient`</mark>](https://developer.apple.com/documentation/swiftui/anygradient) comform to [`View`](/ios/swiftui/view/view.md).
{% 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&amp;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](/ios/swiftui/shapes/shapestyle/gradient.md)
  {% endtab %}
  {% endtabs %}
