swift转场动画_Swift游乐场的演变

swift转场动画

“An amazing platform to learn coding, a great developer tool” is a good definition for Swift Playgrounds, because it summarises two main use cases the platform gives us: on one hand, a way to learn coding (in Swift) with a challenge based approach, where learners are guided in the learning process with incremental steps, concepts and tools, tailored to get programming to more people. On the other hand, a great developer tool to run Swift code on iPad or outside a classic Xcode project/workspace, giving developers the chance to prototype features, easily test interactions which require device capabilities (like accelerometer), isolate issues from large codebase, create learning experiences: in few words, it’s the fastest way to code with Apple’s APIs.

“一个令人赞叹的学习编码平台,一个出色的开发人员工具”对于Swift Playgrounds是一个很好的定义,因为它总结了该平台为我们提供的两个主要用例:一方面,一种基于挑战的学习编码的方法(在Swift中)这种方法,通过逐步增加的步骤,概念和工具来指导学习者学习,量身定制以使更多人参与编程。 另一方面,这是一个出色的开发人员工具, 可以在iPad上或经典Xcode项目/工作区之外运行Swift代码 ,从而为开发人员提供了对功能进行原型制作,轻松测试需要设备功能(例如加速度计)的交互,将问题与大型代码库隔离的机会,创造学习体验:简而言之,这是使用Apple API进行编码的最快方法

In this article, I want to dive into the evolution of playgrounds, from the introduction in 2014 to recent updates, as well as point to some interesting features and use cases.

在本文中,我想深入探讨游乐场的发展,从2014年的介绍到最近的更新,并指出一些有趣的功能和用例。

从Mac应用程序简介 (From the Introduction to the Mac app)

Playgrounds were first introduced in 2014: Xcode > New Playground was the easier way to start experimenting with the brand new Swift language. One year later, Playground in Xcode 7 was updated with inline results, pages, markup. In 2016, the Swift Playgrounds iPad app was launched, with code challenges designed by Apple to learn coding and the new playgroundbook format. From there, the app has gained new features every year: ARKit and camera access, Playground Support and Bluetooth frameworks, most of iOS SDK frameworks, 3rd-party subscriptions, just to mention some of them.

Playgrounds于2014年首次推出: Xcode> New Playground是开始尝试全新Swift语言的更简便方法。 一年后,Xcode 7中的Playground进行了内联结果,页面,标记的更新。 2016年, Swift Playgrounds iPad应用程序 推出后,苹果公司设计了代码挑战来学习编码和新的游乐场书格式。 从那里开始,该应用每年都获得新功能:ARKit和摄像头访问,Playground支持和蓝牙框架,大多数iOS SDK框架,第三方订阅,仅举其中的一些。

With the 3.0 version, modules arrived in Swift Playgrounds: folder of Swift files shared across the book with multiple access control levels available, enabling modularization, scalable and reusable code. While Xcode Playground remains a great developer tool, the Swift Playgrounds app has covered during time not only the learn coding side, but even many developers use cases, as mentioned in the introduction, with a important issue: playground books were running only on iPad. Until few weeks ago, in March 2020, when Swift Playgrounds for Mac was released. And here we are.

在3.0版本中, 模块进入了Swift Playgrounds:Swift文件的文件夹在全书中共享,具有多个可用的访问控制级别,从而实现了模块化,可伸缩和可重用的代码。 尽管Xcode Playground仍然是出色的开发人员工具,但Swift Playgrounds应用程序在一段时间内不仅涵盖了学习编码方面,而且涵盖了许多开发人员用例(如导言中所述),但存在一个重要问题:游乐场书籍仅在iPad上运行。 直到几周前,即2020年3月,Mac版Swift Playgrounds发行了。 我们到了。

Image for post
Swift Playgrounds Mac and iPad apps, Xcode Playground
Swift Playgrounds Mac和iPad应用程序,Xcode Playground

.playground与.playgroundbook (.playground vs .playgroundbook)

As you may already know, a playground usually comes with an editor view on the left, which contains code and (eventually) prose with markup and annotation features; and the live view on the right (if setted). The result sidebar shows a representation of values on corresponding line, customizable using the CustomPlaygroundDisplayConvertible protocol. But, as I wrote above, when we talk about playgrounds, there are two available formats: the .playground and the .playgroundbook. What are the differences?

您可能已经知道,游乐场通常在左侧带有编辑器视图 ,其中包含代码以及(最终)带有标记和注释功能的散文。 以及右侧的实时取景 (如果已设置)。 结果边栏在相应的行上显示值的表示形式,可使用CustomPlaygroundDisplayConvertible协议对其进行自定义。 但是,正如我上面所写,当我们谈论游乐场时,有两种可用格式: .playground.playgroundbook 。 有什么区别?

The .playground is available from the very beginning: it’s the only format compatible between Swift Playgrounds apps and Xcode, enabling seamless workflow. In fact, you can create a .playground right on the iPad, using the “Playground Xcode” template, and then open the file on the Mac, both on Xcode or from the app. With a standard playground you can create multiple pages, write prose with markup, but modules are missing (even if there are shared and page relative sources). It’s the right choice if you need to switch between Xcode and iPad frequently, with full compatibility, having an immediate on-device feedback while implementing features. Some other powerful features of .playground come from the integration with Xcode: you can read more about them in the last section.

.playground从一开始.playground用:它是Swift Playgrounds应用程序和Xcode之间唯一兼容的格式,从而实现了无缝的工作流程。 实际上,您可以使用“ Playground Xcode”模板在iPad上直接创建.playground ,然后在Mac上通过Xcode或从应用程序中打开文件。 在标准游乐场中,您可以创建多个页面,使用标记编写散文,但是缺少模块(即使存在共享的和页面相关的源代码)。 如果您需要经常在Xcode和iPad之间进行切换,并且具有完全的兼容性,并在实现功能时获得即时的设备内反馈,那么这是正确的选择。 .playground其他强大功能来自与Xcode的集成:您可以在上一节中了解有关它们的更多信息。

On the other hand, there’s .playgroundbook: the default format when you create a new document from standalone apps, it has a package-based structure defined by folders and plist files.

另一方面, .playgroundbook :从独立应用程序创建新文档时的默认格式,它具有由文件夹和plist文件定义的基于包的结构。

Image for post
.playgroundbook structure
.playgroundbook结构

It comes with exclusive features, including already mentioned modules, to organize and modularize code; chapters, to group playground pages with the same topic, and cutscenes, which are fullscreen pages (in HTML or Swift) to show introductions or explanations; a key-value persistent storage, to save data and load it across multiple sessions; a glossary, to define terms cited in the page’s prose; and a system of hints, spoilers and solutions to help learners progress through page’s challenges.

它具有专有功能,包括已经提到的模块 ,可以组织和模块化代码。 章节 ,将具有相同主题的运动场页面和过场动画进行分组, 过场动画是全屏页面(HTML或Swift),用于显示简介或说明; 键值持久存储,以保存数据并将其跨多个会话加载; 词汇表 ,定义页面散文中引用的术语; 以及提示,破坏者和解决方案的系统,以帮助学习者应对页面挑战。

With the exception of modules, these features are focused on learning experiences. For this reason, the book format is the right choice if you want to create rich learning experiences and tutorials; but it’s a powerful tool to code and prototype without Xcode as well. As now, Xcode can’t even run a .playgroundbook, even if it’s possible to open a book, navigate and modify its structure and files.

除模块外,这些功能均侧重于学习体验。 因此,如果您想创建丰富的学习经验和教程,则书籍格式是正确的选择。 但是它也是不使用Xcode进行编码和原型设计的强大工具。 像现在.playgroundbook ,即使可以打开一本书,浏览并修改其结构和文件,Xcode甚至都无法运行.playgroundbook

PlaygroundSupport框架 (PlaygroundSupport Framework)

In order to take advantage of all playground’s features, you should use the PlaygroundSupport framework, which enables you, according Apple’s documentation, to:

为了利用所有游乐场的功能,您应该使用PlaygroundSupport框架,根据Apple的文档,该框架使您能够:

Access a playground page and manage its execution, as well as manage live views; access and share persistent data; assess the progress of the learner, update hints, and show success text

访问游乐场页面并管理其执行以及管理实时视图; 访问和共享持久数据; 评估学习者的进度,更新提示并显示成功文本

First, setting the live view: by doing so, playground is able to render your UI stuffs. Live view can be any UIView, a controller, or even SwiftUI View:

首先,设置实时视图:这样,游乐场就可以渲染您的UI内容。 实时视图可以是任何UIView,控制器,甚至是SwiftUI视图:

PlaygroundPage.current.setLiveView(Text("🤓"))

Playground runs in a synchronous way, so it terminates once the last line of code has been executed: this is a problem if you want to work with asynchronous code. To run playground asynchronously, you either set a live view or use the dedicated api:

Playground以同步方式运行,因此一旦执行了最后一行代码,它就会终止:如果要使用异步代码,这是一个问题。 要异步运行Playground,您可以设置实时视图或使用专用的api:

PlaygroundPage.current.needsIndefiniteExecution = true

You can import files in playgrounds and use the Bundle.main api to access them. As said, books are able to persist data across sessions, by get or set:

您可以在操场上导入文件,并使用Bundle.main api访问它们。 如前所述,书籍能够通过get或set在各个会话之间保留数据:

PlaygroundKeyValueStore.current["your-key"]

By default, live views run in the same process of playgroundpage’s code: the good is you can access them directly, the bad is that run command reset everything. Playground book offers the feature to run an always-on live view in a dedicated process: this means it persists through different runs, enabling you to edit and run code on the editor. How to communicate between an always-on live view and the playground page? Let’s see an example: we want to send our mood to an always-on view responsible to display it. To configure an always-on live view, we have to put a Live.swift file inside the playgroundpage, where we call the setLiveView function: unfortunately, we can’t do this on standalone apps, so we need to edit the structure of the book from the Mac or use the Xcode template (see next section for more details). After that, we can send our Mood from the playground page using:

缺省情况下,实时视图以游乐场页面代码的相同过程运行:优点是您可以直接访问它们,缺点是run命令重置所有内容。 Playground图书提供了在专用过程中运行始终在线实时视图的功能:这意味着它可以在不同的运行过程中持续存在,从而使您能够在编辑器上进行编辑和运行代码。 如何在永远在线的实时取景和游乐场页面之间进行通信? 让我们看一个例子:我们想将情绪发送到负责显示它的始终在线视图。 要配置始终在线的实时视图,我们必须将Live.swift文件放置在操场上页面中,我们将其setLiveView函数:不幸的是,我们无法在独立的应用程序上执行此操作,因此我们需要编辑从Mac上预订或使用Xcode模板(有关更多详细信息,请参见下一部分)。 之后,我们可以使用以下方法从操场上页面发送Mood

The full list of types you can send as messagges is available here. To handle messages, just adopt the PlaygroundLiveViewMessageHandler protocol on the object set as live view, in our case a UIViewController:

您可以在此处以消息形式发送的类型的完整列表。 要处理消息,只需对设置为实时视图的对象采用PlaygroundLiveViewMessageHandler协议,在本例中为UIViewController

And we did it! Use Messenger.send(.happy) from the page, run it and the live view will be updated. Similarly, to send messages from the live view to the page, there’s the PlaygroundRemoteLiveViewProxyDelegate protocol: you can find full framework doc here; the Moods book is at the end of this article.

而我们做到了! 从页面上使用Messenger.send(.happy) ,运行它,实时视图将更新。 同样,要将消息从实时视图发送到页面,还有PlaygroundRemoteLiveViewProxyDelegate协议:您可以在此处找到完整的框架文档; 心情书在本文的结尾。

Image for post
The Moods book in action
情绪书在行​​动

独立应用程序,创作书籍,Xcode Playground (Standalone apps, Authoring Books, Xcode Playground)

Other than formats, the tool you decide to use makes the difference in the playground world. With standalone apps, you’re able to create complex books; code from iPad and then continue on Xcode using the .playground format; more in general, to handle many use cases other than learn coding. And of course, it’s the only way to run books.

除了格式以外,您决定使用的工具在游乐场世界中也有所不同。 使用独立的应用程序,您可以创建复杂的书籍; 从iPad发送代码,然后继续使用.playground格式在Xcode上进行编码; 一般而言,除了学习编码之外,还可以处理许多用例。 当然,这是读书的唯一方法

When talking about authoring books with rich learning experiences, you can’t rely just to the app. First, you can’t edit the prose in the editor; well, you can, for example, type /*:This is a sentence*/, close the book, open it again and the sentence will be rendered as prose but… you can’t modify/delete it anymore 🤯. Same happens to annotations like editable regions or hiding: there’s not a rendered-raw markup switch. Moreover, from the apps you can’t create chapter, cutscenes, glossary, hints, handle messages between always-on live view and editor (as we saw above).

在谈论撰写具有丰富学习经验的书籍时,您不能仅仅依靠该应用程序。 首先,您无法在编辑器中编辑散文; 好吧,例如,您可以键入/*:This is a sentence*/ ,关闭书本,再次打开它,该句子将被呈现为散文,但是……您将无法再修改/删除它🤯。 诸如可编辑区域或隐藏之类的注释也是如此:没有渲染原始标记开关。 此外,从应用程序中,您无法创建章节,过场动画,词汇表,提示,也无法处理始终在线的实时视图和编辑器之间的消息(如上所述)。

For these purposes, you have to edit the book by modifying its structure, plists, sources and resources on a code editor. If you want to start a new book from scratch, Apple provides an Author Template: it’s a Xcode project where you create the entire book’s structure and contents. When you build the project, a .playgroundbook is created, ready to be exported. Being integrated in Xcode, the template offers many benefits: for example, an easier way to use storyboards (compiled stoyboards will be packaged in the book) and all book’s authoring tools. Unfortunately, the template fails to build with Xcode 11.5: as now, the 11.1 is the last supported version.

为此,您必须通过在代码编辑器上修改本书的结构,作者,资料和资源来对其进行编辑。 如果您想从头开始创建新书,Apple提供了一个Author Template :这是一个Xcode项目,您可以在其中创建整本书的结构和内容。 在生成项目时,将创建一个.playgroundbook ,准备将其导出。 该模板被集成到Xcode中,具有许多优点:例如,使用情节提要(书中将打包的stoyboard打包)和所有本书的创作工具的简便方法。 不幸的是,该模板无法使用Xcode 11.5生成:到目前为止,11.1是最后一个受支持的版本。

Before the conclusion, Playground in Xcode deserves few more words, because it covers some other powerful use cases for developers. First, before the introduction of CreateML, playground in Xcode was the easier way to train a CoreML model right on the Mac. The editor is able to show formatted or raw markup: a unique feature to create prose and check it immediately. In addition, there’s dedicated api to customize TouchBar contents; and the step by step execution (using the tool on the left of the editor) allows to run single lines of code, having the chance to respond to live data. Playground in Xcode can even import 3rd-party frameworks as showed in a WWDC18 session, in order to show or test framework’s api: having a framework target, drag the framework project into the playground’s window and it will be converted in a workspace, build the framework and you can import it into the playground.

在得出结论之前,Xcode中的Playground不用再多说了,因为它涵盖了开发人员的其他一些强大用例。 首先,在引入CreateML之前,Xcode 的Playground是在Mac上训练CoreML模型的更简便方法。 编辑器能够显示格式化或原始标记 :创建散文并立即检查散文的独特功能。 此外,还有专用的API可自定义TouchBar内容。 逐步执行 (使用编辑器左侧的工具)允许运行单行代码,从而有机会响应实时数据。 Xcode中的Playground甚至可以导入 WWDC18会话中所示 的第三方框架 ,以显示或测试框架的api:具有框架目标,将框架项目拖动到操场的窗口中,它将在工作空间中转换,构建框架。框架,您可以将其导入操场。

Someone pointed to Swift Playgrounds app as a sort of “light Xcode for the iPad”; the release of Swift Playgrounds for Mac (and the rumoured Xcode porting for the iPad?) clarifies one more time they are two different products, with different use cases. But what about next steps? I would love to see an easier way for authoring playground books, from Xcode and, why not, from the apps; maybe the integration with Swift Package Manager (could be really fun); or some brand new APIs in the PlaygroundSupport framework.

有人指出Swift Playgrounds应用是一种“ iPad的轻Xcode”。 Mac版Swift Playgrounds的发布(以及传闻中的iPad的Xcode移植?)进一步说明了它们是两种不同的产品,具有不同的用例。 但是下一步呢? 我希望看到一种更容易的方法,可以从Xcode以及为什么不用应用程序来编写游乐场书籍。 与Swift Package Manager的集成(可能真的很有趣); 或PlaygroundSupport框架中的一些全新API。

Let’s see what news WWDC and future updates will bring to playgrounds 🍿 In the meantime, here’s some useful resources, including the link to download the Moods.playgroundbook sample, full Apple’s documentation and a playlist of WWDC videos about playgrounds. Thanks for reading!

让我们看看WWDC和将来的更新将带给操场什么新闻🍿同时,这里有一些有用的资源,包括下载Moods.playgroundbook示例的链接,完整的Apple文档以及有关操场的WWDC视频的播放列表。 谢谢阅读!

翻译自: https://medium.com/swlh/the-evolution-of-swift-playgrounds-39bf168be8f

swift转场动画

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值