ViewModifier
╱🚧 under construction
Last updated
Was this helpful?
╱🚧 under construction
Last updated
Was this helpful?
⟩ ⟩ ⟩ ViewModifier (protocol)
🚧
問:「❓」
答:
Content 是 body() 的輸入型別(_ViewModifier_Content<Self>
),使用者無法改變,只是為了使用者方便而設的 typealias。
Body 是 body() 的輸出型別,可以由使用者自由指定(但由 compiler 自動辨識),所以是個 associatedtype,也因此 ViewModifier 變成一個 generic protocol。
💾 模板
當 ViewModifier 的好處就是:可以幫原來的 view 加入新的屬性、 變數(例如: .nsfw())。 👉 Sundell ⟩
既然可以直接用 View extension 來改變一個 View,為什麼還需要大費周章弄一個 ViewModifier 呢❓ 👉
還有都有提到一點:「ViewModifier 也是 ,可以自訂一些 @State, @Binding 變數,也可設定支援動畫的屬性」,這些特性是 View extension 辦不到的。
Another interesting fact about ViewModifiers is that it conforms to View protocol (#todo:此訊息的資料來源?). It means you can use inside ViewModifiers property wrappers like @State, @Binding, @Environment, @ObservableObject, and @EnvironmentObject. 👉 - Swift with Majid
in ViewModifer you can create your own with stored properties, including states and other dynamic properties, also make it animatable, etc. 👉 - @Asperi
⟩
⟩
⟩
VINSOL ⟩
Sundell ⟩
Loaf ⟩
objc.io ⟩
Majid ⟩
Sarun ⟩
PeterFriese.dev ⟩
Five Stars ⟩
dev.to ⟩
egeniq ⟩
-
這裡討論了一個有趣的問題:「為什麼需要 ViewModifier?」一般簡單的 view modifier 用 view extension 就可以,複雜一點的、帶有 State 變數的可以另外寫一個 View,為何還需要特別設計一個 ViewModifier?(這裡提出問題,但目前沒人提供答案)
👥 相關: @available, #available