小言_互联网的博客

SwiftUI官方示例入门

771人阅读  评论(0)

SwiftUI项目下载地址

Section 1

Create a New Project and Explore the Canvas
创建一个新项目并探索画布

Create a new Xcode project that uses SwiftUI. Explore the canvas, previews, and the SwiftUI template code.
To preview and interact with views from the canvas in Xcode, ensure your Mac is running macOS Catalina 10.15
创建一个使用SwiftUI的新Xcode项目。浏览画布、预览和SwiftUI模板代码。
要在Xcode中预览和与来自画布的视图交互,请确保您的Mac运行macOS Catalina 10.15

Step 1
Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project.
打开Xcode,在Xcode的启动窗口中单击Create a new Xcode project,或者选 File > New > Project.

Step 2
In the template selector, select iOS as the platform, select the Single View App template, and then click Next.
在模板选择器中,选择iOS作为平台,选择单视图App模板,然后点击Next。

Step 3
Enter “Landmarks” as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the Landmarks project on your Mac.
输入“Landmarks”作为产品名称,选择Use SwiftUI复选框,然后点击Next。选择一个地点来保存项目在您的Mac。

Step 4
In the Project navigator, click to select ContentView.swift.
By default, SwiftUI view files declare two structures. The first structure conforms to the View protocol and describes the view’s content and layout. The second structure declares a preview for that view.
在项目导航器中,单击以选择ContentView.swift。
默认情况下,SwiftUI视图文件声明两个结构。第一个结构符合视图协议,描述了视图的内容和布局。第二个结构声明了该视图的预览。


Step 5
In the canvas, click Resume to display the preview.
Tip
If the canvas isn’t visible, select Editor > Editor and Canvas to show it.
在画布中,单击Resume以显示预览。
提示
如果画布不可见,选择 Editor > Editor and Canvas来显示它。

Step 6
Inside the body property, change “Hello World” to a greeting for yourself.
As you change the code in a view’s body property, the preview updates to reflect your changes.
在body属性中,将“Hello World”更改为自定义。
当您更改视图主体属性中的代码时,预览会更新以反映您的更改。

Section 2

Customize the Text View
You can customize a view’s display by changing your code, or by using the inspector to discover what’s available and to help you write code.
As you build the Landmarks app, you can use any combination of editors: the source editor, the canvas, or the inspectors. Your code stays updated, regardless of which tool you use.
自定义文本视图
您可以通过更改代码来定制视图的显示,或者使用检查器来发现可用的内容并帮助您编写代码。
在构建Landmarks应用程序时,您可以使用任何编辑器的组合:源编辑器、画布或检查器。无论使用哪种工具,您的代码都会保持更新。

Step 1
In the preview, Command-click the greeting to bring up the structured editing popover, and choose Inspect.
The popover shows different attributes that you can customize, depending on the type of view you inspect.
在预览中,命令-单击问候语,打开结构化编辑弹出窗口,并选择Inspect。
根据查看的视图类型,弹出窗口显示可以自定义的不同属性。

Step 2
Use the inspector to change the text to “Turtle Rock”, the name of the first landmark you’ll show in your app.
使用检查器将文本更改为“Turtle Rock”,即你将在应用程序中显示的第一个地标的名称。

Step 3
Change the Font modifier to Title.
This applies the system font to the text so that it responds correctly to the user’s preferred font sizes and settings.
将字体修饰符更改为标题。
这将系统字体应用于文本,以便它正确响应用户的首选字体大小和设置。

To customize a SwiftUI view, you call methods called modifiers. Modifiers wrap a view to change its display or other properties. Each modifier returns a new view, so it’s common to chain multiple modifiers, stacked vertically.
要自定义SwiftUI视图,需要调用称为修饰符的方法。修饰符包装视图以更改其显示或其他属性。每个修饰符都返回一个新视图,所以通常会将多个修饰符垂直堆叠起来。

Step 4
Edit the code by hand to add the foregroundColor(.green) modifier; this changes the text’s color to green.
手工编辑代码,添加foregroundColor(.green)修饰符;这将文本的颜色更改为绿色。
Your code is always the source of truth for the view. When you use the inspector to change or remove a modifier, Xcode updates your code immediately to match.
您的代码始终是视图的真实来源。当您使用检查器更改或删除修饰符时,Xcode会立即更新您的代码以匹配。

Step 5
This time, open the inspector by Command-clicking on the Text declaration in the code editor, and then choose Inspect from the popover. Click the Color pop-up menu and choose Inherited to change the text color to black again.
这一次,通过命令-单击代码编辑器中的文本声明打开检查器,然后从弹出窗口中选择Inspect。单击“颜色”弹出菜单并选择“继承”将文本颜色再次更改为黑色。

Step 6
Notice that Xcode updates your code automatically to reflect the change, removing the foregroundColor(.green) modifier.
注意,Xcode会自动更新代码以反映更改,删除foregroundColor(.green)修饰符。

Section 3

Combine Views Using Stacks
Beyond the title view you created in the previous section, you’ll add text views to contain details about the landmark, such as the name of the park and state it’s in.
When creating a SwiftUI view, you describe its content, layout, and behavior in the view’s body property; however, the body property only returns a single view. You can combine and embed multiple views in stacks, which group views together horizontally, vertically, or back-to-front.
In this section, you’ll use a vertical stack to place the title above a horizontal stack that contains details about the park.
使用堆栈组合视图
除了在前一节中创建的标题视图之外,您还将添加文本视图来包含关于地标的详细信息,比如公园的名称和所在的州。
创建SwiftUI视图时,要在视图的主体属性中描述它的内容、布局和行为;但是,body属性只返回一个视图。您可以将多个视图组合并嵌入到堆栈中,这些堆栈将视图水平、垂直或前后分组在一起。
在本节中,您将使用垂直堆栈将标题置于包含公园详细信息的水平堆栈之上。

You can use Xcode’s structured editing support to embed a view in a container view, open an inspector, or help with other useful changes.
您可以使用Xcode的结构化编辑支持将视图嵌入到容器视图中,打开检查器,或者帮助进行其他有用的更改。

Step 1
Command-click the text view’s initializer to show the structured editing popover, and then choose Embed in VStack.
命令-单击文本视图的初始化器以显示结构化编辑弹出窗口,然后选择嵌入到VStack中。

Next, you’ll add a text view to the stack by dragging a Text view from the library.
接下来,通过从库中拖动文本视图,将文本视图添加到堆栈中。

Step 2
Open the Library by clicking the plus button (+) at the top-right of the Xcode window, and then drag a Text view to the place in your code immediately after the “Turtle Rock” text view.
单击Xcode窗口右上角的+按钮打开库,然后在“Turtle Rock”文本视图之后立即将文本视图拖到代码中的位置。

Step 3
Replace the Text view’s placeholder text with Joshua Tree National Park.
将文本视图的占位符文本替换为Joshua Tree National Park。

Customize the location to match the desired layout.
自定义位置以匹配所需的布局。

Step 4
Set the location’s font to subheadline.
将位置的字体设置为副标题。

Step 5
Edit the VStack initializer to align the views by their leading edges.
By default, stacks center their contents along their axis and provide context-appropriate spacing.
编辑VStack初始化器,使视图的前导边缘对齐。
默认情况下,堆栈将其内容沿轴居中,并提供与上下文相关的间距。

Next, you’ll add another text view to the right of the location, this for the park’s state.
接下来,您将在位置的右侧添加另一个文本视图,这是公园的状态。
Step 6
In the canvas, Command-click on Joshua Tree National Park, and choose Embed in HStack.
在画布中,Command-click on Joshua Tree National Park,然后选择Embed In HStack。

Step 7
Add a new text view after the location, change the placeholder text to the park’s state, and then set its font to subheadline.
在位置之后添加一个新的文本视图,将占位符文本更改为公园的状态,然后将其字体设置为subheadline。

Step 8
To direct the layout to use the full width of the device, separate the park and the state by adding a Spacer to the horizontal stack holding the two text views.
A spacer expands to make its containing view use all of the space of its parent view, instead of having its size defined only by its contents.
为了引导布局使用设备的全宽度,通过在水平堆栈中添加一个间隔来分隔公园和状态,其中包含两个文本视图。
spacer扩展使其包含视图使用其父视图的所有空间,而不是仅由其内容定义其大小。

Step 9
Finally, use the padding() modifier method to give the landmark’s name and details a little breathing room.
最后,使用padding()修饰符给地标名称和细节一些喘息的空间。

Section 4

Create a Custom Image View
With the name and location views all set, the next thing to do is to add an image for the landmark.
Instead of adding more code in this file, you’ll create a custom view that applies a mask, border, and drop shadow to the image.
创建自定义图像视图
名称和位置视图都设置好后,接下来要做的是添加地标的图像。
您将创建一个应用蒙版、边框和投影到图像的自定义视图,而不是在这个文件中添加更多的代码。
Start by adding an image to the project’s asset catalog.
首先将一个图片添加到项目的asset目录中。

Step 1
Find turtlerock.png in the project files’ Resources folder; drag it into the asset catalog’s editor. Xcode creates a new image set for the image.
在项目文件的资源文件夹中查找turtlerock.png;将其拖到asset目录的编辑器中。Xcode为映像创建一个新的映像集。

Next, you’ll create a new SwiftUI view for your custom image view.
接下来,您将为您的自定义图像视图创建一个新的SwiftUI视图。

Step 2
Choose File > New > File to open the template selector again. In the User Interface section, click to select SwiftUI View and click Next. Name the file CircleImage.swift and click Create.
选择File > New > File,再次打开模板选择器。在用户界面部分,单击以选择SwiftUI视图并单击Next。将文件命名为CircleImage.swift并单击Create。

You’re ready to insert the image and modify its display to match the desired design.
您已经准备好插入图像并修改其显示以匹配所需的设计。

Step 3
Replace the text view with the image of Turtle Rock by using the Image(😃 initializer.
步骤3
使用image(
:)初始化器将文本视图替换为Turtle Rock的图像。

Step 4
Add a call to clipShape(Circle()) to apply the circular clipping shape to the image.
The Circle type is a shape that you can use as a mask, or as a view by giving the circle a stroke or fill.
添加对clipShape(Circle())的调用,将圆形裁剪形状应用于图像。
圆形类型是一种形状,您可以将其用作蒙版,或者通过给圆形添加边框或填充来作为视图。

Step 5
Create another circle with a gray stroke, and then add it as an overlay to give the image a border.
创建另一个带有灰色笔划的圆圈,然后将其作为一个覆盖层添加到图像的边框中。

Step 6
Next, add a shadow with a 10 point radius.
接下来,添加一个半径为10的阴影。

Step 7
Switch the border color to white.
This completes the image view.
将边框颜色改为白色。
这样就完成了图像视图。

Section 5

Use UIKit and SwiftUI Views Together
Now you’re ready to create a map view. You can use the MKMapView class from MapKit to render the map.
To use UIView subclasses from within SwiftUI, you wrap the other view in a SwiftUI view that conforms to the UIViewRepresentable protocol. SwiftUI includes similar protocols for WatchKit and AppKit views.
同时使用UIKit和SwiftUI视图
现在您可以创建一个地图视图了。您可以使用MapKit中的MKMapView类来呈现地图。
要使用来自SwiftUI中的UIView子类,你需要将另一个视图封装在一个符合uiview具象协议的SwiftUI视图中。SwiftUI包括用于WatchKit和AppKit视图的类似协议。

To get started, you’ll create a new custom view that can present an MKMapView.
首先,您将创建一个可以呈现MKMapView的新自定义视图。
Step 1
Choose File > New > File, select iOS as the platform, select the SwiftUI View template, and click Next. Name the new file MapView.swift and click Create.
选择 File > New > File,选择iOS作为平台,选择SwiftUI视图模板,点击Next。将新文件命名为MapView.swift并单击Create。

Step 2
Add an import statement for MapKit, and declare UIViewRepresentable conformance for the MapView type.
Don’t worry about the error that Xcode displays; you’ll fix that in the next few steps.
添加MapKit的import语句,并声明MapView类型的UIViewRepresentable 。
不要担心Xcode显示的错误;您将在接下来的几个步骤中解决这个问题。

The UIViewRepresentable protocol has two requirements you need to add: a makeUIView(context:) method that creates an MKMapView, and an updateUIView(:context:) method that configures the view and responds to any changes.
UIViewRepresentable协议有两个您需要添加的需求:一个是创建MKMapView的makeUIView(context:)方法,另一个是配置视图并响应任何更改的updateUIView(
:context:)方法。

Step 3
Replace the body property with a makeUIView(context:) method that creates and returns an empty MKMapView.
用创建并返回空MKMapView的makeUIView(context:)方法替换body属性。

Step 4
Create an updateUIView(:context:) method that sets the map view’s region to the correct coordinates to center the map on Turtle Rock.
创建一个updateUIView(
:context:)方法,该方法将地图视图的区域设置为正确的坐标,以将地图置于Turtle Rock的中心。

When previews are in static mode, they only fully render SwiftUI views. Because MKMapView is a UIView subclass, you’ll need to switch to a live preview to see the map.
当预览处于静态模式时,它们只完全呈现SwiftUI视图。因为MKMapView是一个UIView子类,你需要切换到实时预览来查看地图。

Step 5
Click the Live Preview button to switch the preview to live mode. You might need to click the Try Again or Resume button above your preview.
In a moment, you’ll see a map of Joshua Tree National Park, home of Turtle Rock.
点击实时预览按钮,将预览切换到实时模式。您可能需要单击预览上方的重试或恢复按钮。
过一会儿,你会看到约书亚树国家公园的地图,那是龟岩的故乡。

Section 6

Compose the Detail View
You now have all of the components you need — the name and place, a circular image, and a map for the location.
With the tools you’ve used so far, combine your custom views to create the final design for the landmark detail view.
组合细节视图
现在您已经拥有了所需的所有组件—名称和位置、圆形图像和位置地图。
使用到目前为止使用的工具,结合您的自定义视图来创建地标详细视图的最终设计。

Step 1
In the Project navigator, select the ContentView.swift file.
在项目导航器中,选择ContentView.swift文件。

Step 2
Embed a VStack that holds the three text views in another VStack.
将包含三个文本视图的VStack嵌入到另一个VStack中。

Step 3
Add your custom MapView to the top of the stack. Set the size of the MapView with frame(width:height:).
将您的自定义MapView添加到堆栈的顶部。使用框架设置MapView的大小(宽度:高度:)。

When you specify only the height parameter, the view automatically sizes to the width of its content. In this case, MapView expands to fill the available space.
当只指定height参数时,视图将自动调整为其内容的宽度。在本例中,MapView展开以填充可用空间。

Step 4
Click the Live Preview button to see the rendered map in the composed view.
You can continue editing the view while showing a Live Preview.
单击Live Preview按钮可以在组合视图中看到呈现的地图。
您可以在显示实时预览的同时继续编辑视图。

Step 5
Add the CircleImage view to the stack.
将CircleImage视图添加到堆栈中。

Step 6
To layer the image view on top of the map view, give the image an offset of -130 points vertically, and padding of -130 points from the bottom of the view.
These adjustments make room for the text by moving the image upwards.
要将图像视图层置于地图视图的顶部,请将图像的垂直偏移距设置为-130点,并将视图底部的填充距设置为-130点。
这些调整通过向上移动图像为文本腾出空间。


Step 7
Add a spacer at the bottom of the outer VStack to push the content to the top of the screen.
在外部VStack的底部添加一个间隔,将内容推送到屏幕的顶部。

Step 8
Finally, to allow the map content to extend to the top edge of the screen, add the edgesIgnoringSafeArea(.top) modifier to the map view.
最后,为了允许地图内容扩展到屏幕的顶部边缘,添加edgesIgnoringSafeArea(.top)修改器到地图视图。


转载:https://blog.csdn.net/ILOVEIT954/article/details/102001845
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场