如何模仿Automator的工作流程视图?
我开始开发我的第一个成熟的 Cocoa 应用程序,其中包含一个我希望其行为(和外观)类似于 Automator 的 AMWorkflowView
的视图。
我想要实现的基本功能:
- 子视图的定位
- 以展开/折叠状态显示子视图
- 多选
- 拖放
为了习惯 Cocoa,我从一个自定义的 NSView 开始,它主要服务作为自定义子视图的容器并处理它们的定位和多重选择。 子视图也是 NSView 的子类,并且本身包含可变数量的视图,例如按钮、标签和弹出菜单,因此可以具有不同的高度。 这工作得很好,但在继续之前,我想确保一切都按照 MVC 模式整洁。
我怀疑 Cocoa 中已经有一个类可以促进视图容器的实现,例如 NSCollectionView。 似乎没有(简单)的方法 在 NSCollectionView
中显示不同大小的视图。我应该继续实现我的自定义 NSView (可能使用 NSArrayController 来支持选择和排序),还是有更好的方法?
非常感谢任何帮助
I’m starting to develop my first full-blown Cocoa application containing a view which I would like to behave (and look) similar to Automator’s AMWorkflowView
.
The basic features I’d like to achieve:
- Positioning of subviews
- Display of subviews in expanded / collapsed states
- Multiple selection
- Drag and drop
In order to get accustomed to Cocoa, I started with a custom NSView
which mainly served as a container for the custom subviews and handled their positioning and multiple selection.
The subviews are also subclasses of NSView
, and contain a variable amount of views themselves, like buttons, labels and popup menus, and therefore can have different heights.
This worked quite well, but before going on, I want to make sure to have everything neat and tidy according to the MVC pattern.
I suspect that there already is a class in Cocoa that facilitates the implementation of a view container, like maybe NSCollectionView
.
It seems that there is no (easy) way to display differently sized views in an NSCollectionView
, though. Should I continue implementing my custom NSView
(probably using an NSArrayController
for selection and sorting support), or are there better ways to go?
Any help is much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,答案是你必须自己动手。 NSCollectionView 不允许可变大小的项目(这也排除了展开/折叠状态)。
对于有限数量的项目,您可以相当轻松地完成此任务(您只需要一个容器视图,在要求布局时正确排列子视图,然后您需要确保在情况发生变化时重新布局)。然而,对于许多子视图,您需要注意尽可能高效。这可以从尽可能少地布局开始(例如,仅布局调整大小的视图“之后”的布局),然后变得像缓存原型视图的视觉表示一样复杂,为除视图之外的所有视图绘制缓存的图像(快速!)正在编辑,并且仅使用/定位正在编辑的视图的“真实”视图。
拖放的工作方式与往常一样,但以上都没有说明
NSCollectionView
为您提供的漂亮动画。 :-) 它速度快且动画精美,因为所有子视图都是统一的(因此布局计算快速且简单)。一旦添加不规则尺寸,问题就会变得更加复杂。底线:如果您需要可变大小的视图,
NSCollectionView
将无法工作,您需要自行编写或查找其他人的共享代码,但性能和漂亮的动画并不容易。Unfortunately the answer is you'll have to roll your own.
NSCollectionView
does not allow for variable-sized items (which also rules out expanded/collapsed states).For a limited number of items, you can accomplish this rather easily (you just need a container view that arranges the subviews properly when asked to layout, then you need to make sure you re-layout when things change). For many subviews, however, you'll need to take care to be as efficient as possible. This can start with laying out as little as possible (only those "after" the resized view, for example) and get as complex as caching a visual representation of a prototype view, drawing the cached images (fast!) for all but the view being edited, and only using/positioning a "real" view for the view being edited.
Drag and drop works the same as it always has, but none of the above accounts for the pretty animation
NSCollectionView
gives you. :-) It's fast and beautifully-animated precisely because all the subviews are uniform (so the layout calculations are fast and simple). Once you add irregular sizes, the problem becomes significantly more complicated.The bottom line: If you need variably-sized views,
NSCollectionView
will not work and you'll need to roll your own or find someone else's shared code, but performance and beautiful animation will not be easy.