首页 > 解决方案 > 在自定义单元格中显示进度指示器的 Swift macOS 应用程序中,是否可以同步所有指示器的旋转?

问题描述

我有一个用 Swift / Objective-C 编写的 Cocoa 应用程序,我遇到的问题只是一个细节,但我认为这很重要。我有一个基于视图的NSOutlineView数据源和委托驱动,并且我创建了一个NSTableViewCell只包含旋转进度指示器的自定义。问题是,当应用程序窗口打开时,只有可见的旋转指示器同步旋转。如果我放大窗口并NSOutlineView显示更多单元格,则新出现的旋转指示器不会同步旋转。这在逻辑上是正确的,因为我在NSOutlineView委托方法中实例化了单元格:

outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView?

我的代码如下:

let view = outlineView.makeView(withIdentifier:NSUserInterfaceItemIdentifier.init("activityProgressTableViewCell"), owner: self) as! ProgressIndicatorTableCellView

view.unFinishedStatusIndicator.startAnimation(self)

所以我认为指标不同步是正常的,因为它们在不同的时刻开始旋转。任何人都可以建议一种模式来解决这个问题吗?任何帮助是极大的赞赏。谢谢。

PS 我知道我可以使用 Cocoa Bindings 而不是在指标上调用 startAnimation,这可以让我一次启动所有指标的动画,但是如何处理 NSOutlineView 子项中的旋转指标?

标签: swiftcocoansoutlineview

解决方案


Ok, I have found a possible solution, by using indeed Cocoa Bindings. In my main controller class, I have declared a property like this:

@objc dynamic var shouldStartFoldersIndicators: Bool = true

Then, in interface builder, I bind the animate binding of the progress indicator in the cell to that path of the main controller. Then, In this delegate method:

outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView?

When I need to display the spinning indicator cell, I set the Bool property to false, then to true. Unfortunately, this does not work:

self.shouldStartFoldersIndicators = false
self.shouldStartFoldersIndicators = true

In order to make it work, I had to defer the statements to the next running loop like this:

DispatchQueue.main.async {
   self.shouldStartFoldersIndicators = false
   DispatchQueue.main.async {
        self.shouldStartFoldersIndicators = true
   }
}

This works well, although I am afraid it could slow down the NSOutlineView redrawing. I thank you in advance for any better solution. Thanks


推荐阅读