在 Swift/Objective-C 中监听窗口大小调整事件 [英] Listen for window resize event in Swift / Objective-C

查看:28
本文介绍了在 Swift/Objective-C 中监听窗口大小调整事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以遍历 OSX 中任何应用程序的所有打开窗口并在 Swift 中监听它们的调整大小事件?我想创建自定义窗口管理器,它会根据用户行为移动和调整事件大小——因此,如果用户调整窗口大小,其他窗口会自动重新排列.

Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift? I want to create custom window manager that would move and resize events based on user behavior – so if user resizes a window, other windows get automatically re-arranged.

我是 Haskell,不是 Swift 开发人员,所以我非常感谢任何展示如何实现这种效果的代码片段/示例.(实际上,我会将它用作一种 Haskell -> Cocoa 绑定.)

I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect. (In fact I'll use it as a kind of Haskell -> Cocoa binding.)

我也很想在 Objective-C 中看到解决方案,但在这里 Swift 对我来说更重要.

I would be very interested in seing the solution in Objective-C also, but Swift is more important for me here.

推荐答案

是否可以在 OSX 中遍历任何应用程序的所有打开窗口并在 Swift 中监听它们的调整大小事件?

可以这样做,更好的方法是让您的自定义窗口管理器类实现 NSWindowDelegate 协议并设置所有窗口的 delegate 到您的自定义窗口管理器.这将为您提供所需的所有调整大小和移动信息.

It is possible to do this, a better way would be to have your custom window manager class implement the NSWindowDelegate protocol and set all window's delegate to your custom window manager. This will give you all the resize and move information you require.

我是 Haskell,不是 Swift 开发人员,所以我非常感谢任何展示如何实现这种效果的代码片段/示例.

实现协议方法:

class CustomWindowManager: NSWindowController, NSWindowDelegate {

       // Resize Methods

       func windowWillResize(sender: NSWindow,toSize frameSize: NSSize) -> NSSize {

            // Your code goes here
       }

       func windowDidResize(notification: NSNotification) {

           // Your code goes here
       }
}

然后您可以选择要实现的协议方法,因为协议中的所有方法都是可选的.

You can then just chose which protocol methods you want to implement as all methods in the protocol are optional.

您必须能够识别哪个窗口正在调用委托方法并跟踪由于对初始窗口更改做出反应而移动或调整大小的任何窗口.请参阅此堆栈溢出问题以获取帮助.

You will have to be able to identify which window is calling the delegate method and track any windows you move or resize due to reacting to the initial window change. See this Stack Overflow question for help with this.

要获取当前屏幕上所有窗口的列表,请参阅此 堆栈溢出问题.

For getting a list of all windows currently on screen see this Stack Overflow question.

要获取当前屏幕上您的应用程序的所有窗口的列表,请参阅此堆栈溢出问题.

这篇关于在 Swift/Objective-C 中监听窗口大小调整事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆