前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hello, SwiftUI

Hello, SwiftUI

作者头像
大话swift
发布2019-07-04 11:21:04
1.3K0
发布2019-07-04 11:21:04
举报
文章被收录于专栏:大话swift大话swift

来体验一把声明式语法吧

import SwiftUI

struct ContentView: View {

var body: some View {

VStack {

MapView()

.edgesIgnoringSafeArea(.top)

.frame(height: 300)

CircleImage()

.offset(x: 0, y: -130)

.padding(.bottom, -130)

VStack(alignment: .leading) {

Text("Turtle Rock")

.font(.title)

HStack(alignment: .top) {

Text("Joshua Tree National Park")

.font(.subheadline)

Spacer()

Text("California")

.font(.subheadline)

}

}

.padding()

Spacer()

}

}

}

#if DEBUG

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

ContentView()

}

}

#endif

我们看看先分析一下它是怎么玩的

上面代码中诡异的出现啦几个陌生的东东

var body: some View {

struct ContentView: View {

PreviewProvider

那么能够时时预览那么肯定跟这三个哥们有关系

下面我们先看看这View在swiftUI中是何方神

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)

public protocol View : _View {

/// The type of view representing the body of this view.

///

/// When you create a custom view, Swift infers this type from your

/// implementation of the required `body` property.

associatedtype Body : View

/// Declares the content and behavior of this view.

var body: Self.Body { get }

}

和明显这个所谓的body就是我们实时预览的真正实体

接着我们看PreviewProvider

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)

public protocol PreviewProvider : _PreviewProvider {

/// The type of the previews variable.

associatedtype Previews : View

/// Generates a collection of previews.

///

/// Example:

///

/// struct MyPreviews : PreviewProvider {

/// static var previews: some View {

/// return Group {

/// GreetingView("Hello"),

/// GreetingView("Guten Tag"),

///

///ForEach(otherGreetings.identified(by: \.self_)) {

/// GreetingView($0)

/// }

/// ]

/// .previewDevice("iPhone X")

/// }

/// }

static var previews: Self.Previews { get }

/// Returns which platform to run the provider on.

///

/// When `nil`, Xcode infers the platform based on the file the

/// `PreviewProvider` is defined in. This should only be provided when the

/// file is in targets that support multiple platforms.

static var platform: PreviewPlatform? { get }

}

咋看之下混混然,仔细一看有一个 static var previews: Self.Previews { get } 还是个static变量,从而我们可以臆断这个就是渲染的主要地方和实现

接下来我们看看怎么在开发中使用

在使用之前我们会发现多了个类

SceneDelegate

代码语言:javascript
复制

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Use a UIHostingController as window root view controller
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }

细心的朋友会发现多个了UIHostingController

而Appdelegate则

代码语言:javascript
复制

    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

自从新的功能就基本串联起来,后续我研究怎么使用swiftUI

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档