深度式睡眠潜入虚拟世界
SwiftUI is the most exciting news since Apple has announced Swift in 2014. It’s an enormous step towards Apple’s goal of getting everyone coding, simplifying the basics so we can spend more time on a custom features that delights our users.
自从苹果公司在2014年发布Swift以来,SwiftUI是最令人振奋的消息。这是朝着苹果的目标迈出的一大步,该目标是让所有人都编码,简化基础,以便我们可以花更多时间在使用户满意的自定义功能上。
SwiftUI framework and design tools work together to enable a new way to build a user interface. Before Xcode 4, IB and Xcode were separate apps and seam can be observed when we edit or delete any IBAction or IBOutlet from our code and run the build. What happens next is a nightmare for developers. Yeah, the right app gets crashed and the reason behind this is that IB doesn’t see changes to the code.
SwiftUI框架和设计工具一起工作以启用一种新的方式来构建用户界面。 在Xcode 4之前,IB和Xcode是单独的应用程序,当我们从代码中编辑或删除任何IBAction或IBOutlet并运行构建时,可以观察到接缝。 接下来发生的事情是开发人员的噩梦。 是的,正确的应用程序崩溃了,其原因是IB看不到代码更改。
But now with SwiftUI our code will always in our sync with design and preview canvas. SwiftUI lets us ignore IB and storyboard. If we are wondering whether we can use SwiftUI with xib and storyboard together then the answer is YES. SwiftUI doesn’t replace UIKit. We can use both in the same app just like Swift and objective-c can be used in the same project.
但是现在使用SwiftUI,我们的代码将始终与设计和预览画布保持同步。 SwiftUI让我们忽略IB和情节提要。 如果我们想知道是否可以将SwiftUI与xib和Storyboard一起使用,那么答案是肯定的。 SwiftUI不会取代UIKit。 我们可以在同一个应用程序中使用两者,就像Swift和Objective-c可以在同一项目中使用一样。
SwiftUI compiler build and run continuously, so we can preview a SwiftUI view side-by-side with it’s code. A change to one side will update the other side, so they are always in sync.
SwiftUI编译器会不断构建和运行,因此我们可以与它的代码并排预览SwiftUI视图。 更改一侧将更新另一侧,因此它们始终保持同步。
SwiftUI works for the majority of iOS devices. If we have designed our component for any specific device say iPhone then the same component can be used on other platforms with minimal code changes and we can reuse a lot of the same components. For e.g. we want to reuse our picker-view designed for iPhone on iPad or mac then this could be easily done by doing some changes in code like iPhone picker-view is like a list whereas for iPad it is pop-up and for mac, it is keyboard shortcut.
SwiftUI适用于大多数iOS设备。 如果我们为任何特定的设备(例如iPhone)设计了组件,那么该组件就可以在其他平台上使用,而无需进行最少的代码更改,就可以重用很多相同的组件。 例如,我们要重用专为iPad或Mac上的iPhone设计的选取器视图,则可以通过对代码进行一些更改来轻松实现,例如,iPhone选取器视图就像一个列表,而对于iPad则是弹出窗口,对于Mac,它是键盘快捷键。
The philosophy for SwiftUI is not to write once, apply everywhere but to learn once, apply anywhere.
SwiftUI的哲学不是只写一次,在任何地方都适用,而是一次学习,在任何地方都适用。
1. Declarative Syntax:
1.声明式语法:
With the help of declarative syntax, we can simply state what our UI should do. For example, we can write that we want an image with a circular shape having a white color border of width 5 pixels and also there should be a shadow of 15 pixels radius.
借助声明性语法,我们可以简单地声明UI应该做什么。 例如,我们可以这样写:我们想要一个具有白色边框的宽度为5像素的圆形图像,并且阴影的半径应该为15像素。
struct CircularImage: View { var body: some View { Image("swiftUILogo") .clipShape(Circle()) .overlay( Circle().stroke(Color.white, lineWidth: 5)) .shadow(radius: 15) }}This code snippet gives us an overview of the actual implementation of the above requirement. See how beautifully it is written with the help of SwiftUI and declarative syntax.
此代码段概述了上述要求的实际实现。 看看它在SwiftUI和声明性语法的帮助下编写得多么精美。
Declarative style even applies to complex concepts like animations.
声明式样式甚至适用于复杂的概念,例如动画。
Easily add or remove animations to almost any control and choose a collection of ready to use effects with only a few lines of code. At runtime, the system handles all of the steps needed to create a smooth movement and even deals with interruption to keep our app stable. With such type of feature, we will be looking for new ways to make our app come alive.
轻松地向几乎所有控件添加或删除动画,并仅需几行代码即可选择一组随时可用的效果。 在运行时,系统将处理创建平稳运动所需的所有步骤,甚至处理中断以保持我们的应用稳定。 借助此类功能,我们将寻找使我们的应用活跃起来的新方法。
SwiftUI works seamlessly with the new Xcode design tool to keep our code and design perfectly in sync. Automatic support for dynamic type, dark mode, localization, and accessibility are some of its highlights.
SwiftUI与新的Xcode设计工具无缝协作,以使我们的代码和设计完美同步。 自动支持动态类型,暗模式,本地化和可访问性是它的一些亮点。
With the help of design tool our first line of SwiftUI code is already the most powerful UI code we have ever written.
借助设计工具,第一行SwiftUI代码已经是我们编写过的功能最强大的UI代码。
2.1 Drag and Drop
2.1拖放
We can arrange components within our UI by simply dragging the control on the canvas. We can also drag and control from our library and drop then on the design canvas or directly in the code.
我们只需在画布上拖动控件即可在UI中安排组件。 我们还可以从库中拖放控件,然后将其拖放到设计画布上或直接在代码中。
2.2 Dynamic Replacement
2.2动态替换
The Swift compiler and runtime are fully embedded throughout Xcode, so our app is constantly being built and run. The design canvas isn’t just an approximation of our UI but it’s the live app and Xcode swap edited code directly in our live app with dynamic replacement.
Swift编译器和运行时完全嵌入到整个Xcode中,因此我们的应用程序不断地构建和运行。 设计画布不仅仅是我们的UI的近似值,它是实时应用程序和Xcode在动态应用程序中直接通过动态替换交换已编辑的代码。
In the above visual, we can see how Preview canvas automatically getting updated.
在以上视觉效果中,我们可以看到预览画布如何自动更新。
2.3 Modifiers
2.3修饰符
We can easily drag and drop modifiers next to our views and they can be enhanced with new properties such as colors, transforms, visual effects, gestures, and many more.
我们可以轻松地将修改器拖放到视图旁边,并可以使用新属性(例如颜色,变换,视觉效果,手势等)来增强它们。
2.4 SF Symbols
2.4 SF符号
We can download the newest SF fonts from Apple website. Apple has provided more than 1000 icons in their new SF symbols app which can be used as vector assets in Xcode or in the design tool
我们可以从Apple网站下载最新的SF字体。 苹果在其新的SF Symbols应用程序中提供了1000多个图标,可用作Xcode或设计工具中的矢量资产
Conclusion
结论
It will be more straightforward to adapt SwiftUI as Swift keeps evolving and there are more and more new features that can help us to improve the productivity of the codebase.
随着Swift的不断发展,采用SwiftUI将变得更加直接,并且越来越多的新功能可以帮助我们提高代码库的生产力。
Any comments and feedback are welcome, so please share your thoughts. Thank you!
欢迎任何评论和反馈,请分享您的想法。 谢谢!
翻译自: https://medium.com/globant/dive-into-amazing-world-of-swiftui-f35a25c5e9ce
深度式睡眠潜入虚拟世界
相关资源:完整版秘密潜入OICQ1.0.rar