View簡(jiǎn)介
A type that represents part of your app’s user interface and provides modifiers that you use to configure views.
View
代表應(yīng)用程序用戶界面的一部分并提供用于配置視圖的修飾符
public protocol View : _View {
associatedtype Body : View
var body: Self.Body { get }
}
View
協(xié)議有一個(gè)關(guān)聯(lián)的類型 Body
懂拾,它被限制為任何符合 View
協(xié)議的類型辣卒。body
中是在屏幕上呈現(xiàn)的實(shí)際內(nèi)容脖镀。 SwiftUI 將通過(guò) body
計(jì)算屬性的實(shí)現(xiàn)來(lái)推斷關(guān)聯(lián)的類型椭懊。
You create custom views by declaring types that conform to the
View
protocol. Implement the requiredbody
computed property to provide the content for your custom view.
你可以通過(guò)聲明符合View
協(xié)議的類型來(lái)創(chuàng)建自定義視圖恳啥。實(shí)現(xiàn)所需的 body
計(jì)算屬性以為您的自定義視圖提供內(nèi)容,如下:
struct MyView: View {
var body: some View {
Text("Hello, World!")
}
}
some View
這種寫法使用了 Swift 5.1 的 Opaque return types
特性驮俗。它向編譯器作出保證西乖,每次 body
得到的一定是某一個(gè)確定的,遵守 View
協(xié)議的類型简僧,告訴編譯器“不要再細(xì)究具體的類型"
Assemble the view’s body by combining one or more of the built-in views provided by SwiftUI, like the
Text
instance in the example above, plus other custom views that you define, into a hierarchy of views. For more information about creating custom views, see Declaring a custom view.
通過(guò)將 SwiftUI 提供的一個(gè)或多個(gè)內(nèi)置視圖(如上例中的 Text 實(shí)例)以及你定義的其他自定義視圖組合到視圖層次結(jié)構(gòu)中來(lái)組裝視圖的body
建椰。有關(guān)創(chuàng)建自定義視圖的更多信息, 可以看Declaring a custom view.
The
View
protocol provides a set of modifiers — protocol methods with default implementations — that you use to configure views in the layout of your app. Modifiers work by wrapping the view instance on which you call them in another view with the specified characteristics, as described in Configuring views. For example, adding theopacity(_:)
modifier to a text view returns a new view with some amount of transparency:
View
協(xié)議提供了一組修飾符 - 具有默認(rèn)實(shí)現(xiàn)的協(xié)議方法 - 您可以使用它們來(lái)配置應(yīng)用布局中的視圖岛马。修飾符通過(guò)將調(diào)用它們的視圖實(shí)例包裝在具有指定特征的另一個(gè)視圖中來(lái)工作棉姐,如配置視圖 中所述。例如啦逆,將 opacity(_:)
修飾符添加到文本視圖會(huì)返回一個(gè)包含一定數(shù)量的新視圖透明度:
Text("Hello, World!")
.opacity(0.5) // Display partially transparent text.
Why is View a struct and not a class anymore?
One of the fundamental principles of SwiftUI is to have a single source of truth in your code. A traditional UIKit approach of having a base UIView class and all other views inherit from it has a demerit wherein we have multiple stored properties in our view inherited from parent UIView. You only define properties that you want to use unlike in UIKit where you inherit a number of properties from your parent view.
SwiftUI
的基本原則之一是在你的代碼中擁有單一的事實(shí)來(lái)源伞矩。傳統(tǒng) UIKit 方法有一個(gè)基類 UIView
,所有從它繼承而來(lái)視圖都有一個(gè)缺點(diǎn)蹦浦,即我們的視圖中有多個(gè)存儲(chǔ)屬性扭吁,這些屬性是從父 UIView
繼承的。而在SwiftUI
中你只定義你想使用的屬性盲镶,不像在UIKit
中你從父視圖繼承許多屬性
SwiftUI tends to make views pretty lightweight and what better option than a value type with no headache of reference counting and retain cycles. Reference types are messier to maintain. You can alter your properties from anywhere in your code.
Swift UI also makes you be responsible for any views and their own state. Views are independent and isolated from one another. Any changes to a particular view will not affect any other unless they are binded by some common source of truth. It’s allocated on the stack, and it’s passed by value.
SwiftUI
傾向于使視圖變得非常輕量級(jí)侥袜,并且有什么比值類型是更好的選擇,無(wú)需擔(dān)心引用計(jì)數(shù)和循環(huán)引用溉贿。引用類型更難維護(hù)枫吧,因?yàn)槟憧梢詮拇a中的任何位置更改屬性。
SwiftUI
讓你對(duì)任何視圖和它們自己的狀態(tài)負(fù)責(zé)宇色。視圖是獨(dú)立的并且彼此隔離九杂。對(duì)特定視圖的任何更改都不會(huì)影響任何其他視圖,除非它們受到某些共同事實(shí)來(lái)源的約束宣蠕。它在堆棧上分配例隆,并按值傳遞。
Simple Example
屏幕上展示一個(gè)文本內(nèi)容抢蚀,直接使用Text
即可
import SwiftUI
// 定義結(jié)構(gòu)體ContentView镀层,遵守View協(xié)議
struct ContentView: View {
// 計(jì)算屬性body
// some View:表示返回的內(nèi)容將遵守View協(xié)議
var body: some View {
// 返回一個(gè)具體類型的View
Text("Hello, World!")
}
}
// 僅僅是用于預(yù)覽界面效果,最終不會(huì)被打包
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
可以在右側(cè)的預(yù)覽頁(yè)面看到效果:
self is immutable in View body
struct ContentView: View {
var title: String
var body: some View {
VStack {
Button(action: {
self.title = "I am changing this"
}) {
Text("Hit me")
}
}
}
}
上面代碼皿曲,我們嘗試修改title唱逢,將會(huì)得到編譯錯(cuò)誤提示:
這個(gè)錯(cuò)誤是因?yàn)?code>body是一個(gè)計(jì)算屬性,我們不能從計(jì)算屬性中改變 self
屋休。那么如何解決坞古?可以使用@State
修飾,將title
作為 @State
屬性劫樟,那么 SwiftUI
框架會(huì)負(fù)責(zé)對(duì)其存儲(chǔ)并且可以修改痪枫。
@State var title: String