在Xcode中实现TouchBar功能。

时间:2023-01-23 19:12:31

How can I implement a NSTouchBar in using only Xcode Playgrounds? I realize that NSTouchBar, and its accompanying methods including makeTouchBar() are contained within the NSResponder class, which is the superclass of NSView, NSViewController, NSWindow, and SKView (a subclass of NSView.)

如何只使用Xcode游乐场实现NSTouchBar ?我意识到NSTouchBar及其伴随的方法包括makeTouchBar()包含在NSResponder类中,它是NSView、NSViewController、NSWindow和SKView (NSView的子类)的超类。

It is for this reason that I'm a little unsure how to approach access the TouchBar. There are so many subclasses of NSResponder I'm unsure as to which one is the correct one. And to my knowledge I cannot even access all of them, including the NSWindow for the Playground, which I suspect may be the one I need to access.

正是因为这个原因,我有点不确定如何接近TouchBar。NSResponder有很多子类我不确定哪一个是正确的。据我所知,我甚至不能访问所有的窗口,包括运动场的NSWindow,我怀疑这可能是我需要访问的窗口。

My current setup is:

我目前的设置是:

let containerView = SKView()
PlaygroundPage.current.liveView = containerView
let containterScene = SKScene()
containerView.presentScene(containterScene)

And in checking the responder chain, for the SKView I get:

在检查responder chain时,对于SKView我得到:

<SKScene> name:'(null)' frame:{{-250, -250}, {500, 500}} anchor:{0.5, 0.5}

And for the SKScene:

和SKScene:

<SKView: 0x7fba36037a00>
<NSView: 0x7fba32d6fe80>
<PlaygroundViewBridgeService: 0x7fba32d569e0>
<NSNextStepFrame: 0x7fba32d76e20>
<NSWindow: 0x7fba32e177e0>

For some reason, the SKScene and SKView are not within the same chain, and I can't seem to access any responder higher than the SKView. Is there a way to extend the functionality of the already existing NSWindow?

由于某些原因,SKScene和SKView不在同一个链中,我似乎无法访问比SKView更高的任何响应程序。是否有办法扩展现有的NSWindow的功能?

Another problem is that many tutorials on using the TouchBar require access to the AppDelegate, which I don't think I have in Xcode Storyboards.

另一个问题是,关于使用TouchBar的许多教程都要求访问AppDelegate,我认为在Xcode故事板中没有这个功能。

Any help implementing the TouchBar in Storyboards would be greatly appreciated.

在故事板中实现TouchBar的任何帮助都将非常感谢。

1 个解决方案

#1


1  

The solution is actually quite simple. Create a custom subclass of NSViewController and implement the TouchBar override functions within it.

解决方法其实很简单。创建NSViewController的自定义子类,并在其中实现TouchBar覆盖函数。

Then implement the above code:

然后执行上述代码:

let containerView = CustomSKView()
let containterScene = CustomSKScene()
containerView.presentScene(containerScene)

With the following additions.

与下面的添加。

let containerViewController = CustomNSViewController()
containerViewController.view = containerView
PlaygroundPage.current.liveView = containerViewController.view

This sets your custom NSViewController within the responder chain, allowing it to take care of the Touch Bar. There is also no need to worry about the App Delegate.

这将在responder chain中设置您的自定义NSViewController,允许它处理触摸栏。也不需要担心App委托。

#1


1  

The solution is actually quite simple. Create a custom subclass of NSViewController and implement the TouchBar override functions within it.

解决方法其实很简单。创建NSViewController的自定义子类,并在其中实现TouchBar覆盖函数。

Then implement the above code:

然后执行上述代码:

let containerView = CustomSKView()
let containterScene = CustomSKScene()
containerView.presentScene(containerScene)

With the following additions.

与下面的添加。

let containerViewController = CustomNSViewController()
containerViewController.view = containerView
PlaygroundPage.current.liveView = containerViewController.view

This sets your custom NSViewController within the responder chain, allowing it to take care of the Touch Bar. There is also no need to worry about the App Delegate.

这将在responder chain中设置您的自定义NSViewController,允许它处理触摸栏。也不需要担心App委托。