Notification while driving NSWindow

How to get a notification when a position is changed NSWindowby dragging its title bar? I know that I can use notifications windowWillMove:and windowDidMove:, but this will give me a notification only when starting and ending a drag.

+3
source share
3 answers

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) { // make sure we have the right window
        self.dragCursorStartPos = [NSEvent mouseLocation];
        const NSTimeInterval dragDelaySeconds = 0.1; // polling rate delay
        self.dragWindowTimer = [NSTimer scheduledTimerWithTimeInterval:dragDelaySeconds
                                                                target:self
                                                              selector:@selector(myMethod)
                                                              userInfo:nil
                                                               repeats:YES];
    }
}

, .

- (void)windowDidMove:(NSNotification *)notification {
    if (notification.object == self.view.window) { // make sure we have the right 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). , .

+2

Cocoa, AFAIK windowDidMove , , ( , ).

: , , , . , .

0

I would recommend looking at this link: http://www.cocoabuilder.com/archive/cocoa/31183-nswindow-not-updating-position-when-being-dragged.html

The responder says that you can use the event windowWillMoveto start a timer that should trigger updateWindow(which seems critical here), then you can periodically read the property of the frame that needs to be updated, and then stop your timer on windowDidMove.

0
source

All Articles