Dosen原创
这是我学习quartz composer,Origami,看官方文档翻译下来的,每天一篇,有不准确的地方希望能指出,一起交流进步。转载请与我联系,擅自转载视作侵权。
Iterators 迭代器
Iterators are an easy way to minimize repetitive patches that do the same thing. They are similar to loops in programming. A common reason to use Iterators is to display many of the same Layers, with different attributes (e.g. a News Feed). This way if you update one Layer you update all of them at the same time — it will make your prototyping much faster.
Iterators是让做同样事情的重复的模块最小化的最简单的方法。它们和程序中的循环很类似。使用迭代器的一个常见原因是用不同的属性显示很多的同样的层。这个方法,如果你更新一个层就会同时更新所有的,它会让你的原型工作更快速。
Using Current Index to build repeating layouts 使用当前的索引建立重复的布局
The foundation of working with Iterators is to using the Current Index value from Iterator Info. Lets say you have an Iterator with a count of 10. Iterator basically clones all the contents 10 times, and for each clone it provides a different index from Iterator Info (where the first clone has an index of 0, second clone has an index of 1, and so on until the 10th item with an index of 9).
使用迭代器工作的框架是使用Iterator Info里当前的索引值。比方说你有一个迭代10次的迭代器,迭代器克隆所有包含的内容10次,每个克隆都提供一个Iterator Info里的不同的索引(第一个克隆的索引是0),第二个是1,直到最后一个索引是9。
In practice, one of the most common modifications to Current Index is multiplying it by an offset to place stack items horizontally or vertically. For example, if you were prototyping the Instagram feed, where each feed item is about the same height (let's say 850px for a 750px image and 100px for padding and author and caption), we would connect a Math patch to Current Index, set to multiply by -850px (to repeat downwards), and plug that into the Y Position of the Layer patch rendering the feed item. This is what that might look like inside an Iterator:
事实上,当前索引的一个最常见的修改是乘以一个偏移值,使一些东西可以水平的或垂直的摆放成一列或一排。举例:如果你在做一个图片分享的排序,每一个单元都是同样的高度(比如说是850px,一个750px的图,并且和作者标题间有100px的间距),我们会给当前索引连接一个数学模块,设置乘以-850px(重复向下),并且把它插入层模块的Y Position来渲染排序的单元。这是在一个迭代器里看起来会是什么样的。
Using Current Index to have dynamic content 使用当前索引创建动态内容
Current Index is also very useful to handle displaying dynamic content from a structure.
当前索引对于处理从结构体动态显示内容很有用。
Let's say we have a list of friends. For each friend you want to have a different name. In an Iterator, you cannot manually input the text for each Text Layer. However, if you create a structure of names outside the Iterator (With Structure Creator set to String), you can pass in that structure of names and use Structure Index Member with Current Index to get each name for each index.
加入我们有一个朋友列表。每个朋友你想有不同的名字。在一个迭代器里,你不能手动地为每一个文字层输入文字。然而,如果你创建在迭代器的外面创建一个名字的结构体(把Structure Creator设置为字符串类型),你可以通过名字的结构体,并且使用Structure Index Member和当前索引来获取到每个索引对应的名字。
Challenges with Iterators
There are several caveats with Iterators that can be limiting, and one crash-inducing issue. However, iterators are helpful enough to justify their use even with these issues.
有几个迭代器的注意事项,可能会有一些限制或者引发崩溃的问题。然而,即使有这些问题,迭代器仍然是非常有用的。
Changing the Iteration count may cause a crash 改变迭代器数量可能引发崩溃
If an Iterator has a Layer patch within, and you change the Iterator count while the composition is running, it will most likely crash. This is due to a bug with a patch called Feedback inside Layers, which helps snap layers to pixels and is difficult to fix at the moment.
如果一个迭代器和一个层模块一起使用,并且当合成器在运行时你改变了迭代器数量,它很可能会崩溃。这归咎于在层里模块的一个叫做Feedback的漏洞。
Solution: Stop the composition before changing the Iterator count. If you wish to have a dynamic Iterator count (e.g. an interaction will cause new items to be displayed in a News Feed), set the count to the highest it can be, and use Conditionals inside the Iterator to hide the inactive Layers based on the index.
解决方案:在改变迭代器数量之前停止合成器。如果你希望有一个动态的迭代器数量(例如:在一个动态消息中,一个交互动作会产生新的单元被显示),设置它可达到的最大数量,并基于索引在迭代器中使用条件来隐藏没用的层。
Only the last iteration values are visible in the editor 在编辑器中只有最后迭代的值是被显示的
Following the patch values in your composition is useful to help debug a problem. However, Iterators only show the values for the last iteration (e.g. if you have 10 iterations, it only shows the values for the 10th one). There isn't a real workaround for this, except changing the Iteration count or setting a Max on the index.
在合成器中追踪模块的值对于debug一个问题是非常有用的。然而,迭代器只能显示最后迭代的值(比如:如果你迭代10次,它只显示第10个值)。这不是一个真实的运行环境,除了改变迭代次数或设置索引的最大值。
Passing values out of Iterators is difficult 把值传递出迭代器是困难的
There are two different methods depending on if your Iterator has a blue consumer patch or not:
根据你的迭代器有或没有一个蓝色的模块,这有两种不同的方法:
No consumer patch inside 迭代器里没有蓝色模块
Lets say you want to pass a number out of an Iterator. If you publish it (right click the patch and select an output to publish), how does the Iterator know which value to pass out? The answer is to pass all of the values out as a structure and pick the one you want from within.
假如你想将一个数传递出迭代器。如果你公开它(模块右键并选择一个输出来公开),迭代器如何知道你想讲哪个值传递出去?答案是把所有值作为一个结构体传递出去,你想要的那个值也在其中。
To create a structure of the values within the Iterator, we can use the Queue patch, which works very well with Iterators if the Queue size is the same as the Iterator count. Simply enable Filling, and pass in the value you want to store. Then publish the Queue's output structure. Now you can access the value you want outside of the Iterator with a Structure Index Member .
用迭代器的值创建一个结构体,我们可以使用Queue模块,如果Queue的大小和迭代器次数一样的话,它和迭代器一起非常好用。只需要启用填充,并且进入你想要储存的值,然后公开Queue输出的结构体。现在你可以使用一个Structure Index Member访问这个你想要在迭代器外面的值。
For example, if we wish to pass out the Y Position values we calculated when laying out the example Instagram feed at the top of this page, we would take the output of that Math patch and fill it into a Queue, and then publish it by right clicking on the patch and selecting the queue port, or hovering on the queue output and hittingP:
例如,我们希望将这个图片分享案例页面顶部的我们计算出的Y坐标值传递出来,我们会使Math模块的输出连接进一个Queue(队列),并在模块右键选择queue接口公开它,或者悬停在queue输出上按P键。
Then, from the outside of the iterator, access any member within the queue, or pass it to another iterator for more processing.
这样,从迭代器的外面,使用queue访问任意成员,或传递它到另一个迭代器进行更多运算。
Consumer patches inside 迭代器里有蓝色模块
Due to the QC limitation that blue consumer patches cannot have any outputs, you have to use a Wireless Broadcaster to pass any values out. The Wireless Broadcaster doesn't work 100% properly in an Iterator (only passes the last index) and will require a workaround before it can pass out a value from a specific index that is not the last one.
由于QC中蓝色模块没有任何输出这样的限制,你只能使用无线广播将值传递出去。无线广播不是100%适用于迭代器中(只有传递最后索引时不适用),在从指定的索引传递一个值出去之前它对运行环境的一个要求是不可以是最后一个索引值。
The basic concept of the workaround is to only enable the Wireless Broadcaster for the index that you want to pass a value out of. Wireless Broadcasters have a hidden Enable port that you can use by right-clicking and inserting an input splitter for Enable. This is an example where we want the value for the 6th iteration (index 5), after inserting the input splitter for Enable:
运行环境的基本概念是,无线广播只对你传递出索引的值有用。无线广播有一个隐藏启用接口,你可以通过右键点击并插入一个输入分流器来启用。这是一个例子,在插入一个启用的输入分流器后,我们想要这个值迭代6次(索引值是5)。
You can also combine Wireless Broadcaster with the Queue implementation above.
你也可以用Wireless Broadcaster结合Queue实现上面展示的。