MacOS开发 鼠标拖动窗口事件

参考官方文档:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/WinPanel/Tasks/SizingPlacingWindows.html

NSWindow有一个属性movable,默认是YES,此时属性movableByWindowBackground是可用的;当movable被置为NO,movableByWindowBackground属性被忽略。movableByWindowBackground如果为YES,则用户可通过点击并且拖拽window的背景实现拖动窗口。但是有种情况会失效,需要用以下代码(继承NSView的自定义类)覆盖mouseDownCanMoveWindow方法,并且返回YES。我遇到的情况是,我在window的view上添加了一个自定义的NSSplitView类CustomSplitView,导致鼠标拖动背景事件无效,因此在类CustomSplitView添加此方法即可解决拖动不了的问题。
/**
The user can generally reposition windows by dragging only the title bar. If you want users to be able to drag your window by clicking elsewhere, you should override mouseDownCanMoveWindow so that it returns YES in any views that you want to be draggable window regions. The methods isMovable and setMovable: determine whether the user can move the window by clicking in its title bar or background.

@return
*/
-(BOOL)mouseDownCanMoveWindow{
[super mouseDownCanMoveWindow];
return YES;
}

你可能感兴趣的:(MacOS开发 鼠标拖动窗口事件)