I have a solution that allows you to determine the position of a window while dragging it.
Two problems are that there is no built-in way to receive notifications while dragging a window, and that the window frame is not updated until it stops moving. My approach works around these issues by setting up a repeating timer and tracking cursor movement.
NSWindowWillMoveNotification NSWindowDidMoveNotification, , .
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillMove:)
name:@"NSWindowWillMoveNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidMove:)
name:@"NSWindowDidMoveNotification"
object:nil];
- , , " ".
- (void)windowWillMove:(NSNotification *)notification {
if (notification.object == self.view.window) {
self.dragCursorStartPos = [NSEvent mouseLocation];
const NSTimeInterval dragDelaySeconds = 0.1;
self.dragWindowTimer = [NSTimer scheduledTimerWithTimeInterval:dragDelaySeconds
target:self
selector:@selector(myMethod)
userInfo:nil
repeats:YES];
}
}
, .
- (void)windowDidMove:(NSNotification *)notification {
if (notification.object == self.view.window) {
if (self.dragWindowTimer != NULL) {
[self.dragWindowTimer invalidate];
self.dragWindowTimer = NULL;
}
}
}
/ , , , .
- (void)myMethod {
NSPoint cursorPos = [NSEvent mouseLocation];
NSPoint cursorDisplacement = NSMakePoint(cursorPos.x - self.dragCursorStartPos.x, cursorPos.y - self.dragCursorStartPos.y);
CGPoint frameOrigin = self.view.window.frame.origin;
CGPoint actualFrameOrigin = CGPointMake(frameOrigin.x + cursorDisplacement.x, frameOrigin.y + cursorDisplacement.y);
NSLog(@"The frame actual origin is (%f, %f)", actualFrameOrigin.x, actualFrameOrigin.y);
}
actualFrameOrigin myMethod , , self.view.window.frame.origin .
, !
, , , NSWindowWillMoveNotification, NSWindowDidMoveNotification, . , , myMethod, (pressedButtons & (1 << 0)) == (1 << 0). , .