Difference between revisions of "NSView"
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
=== Dragging objects === | === Dragging objects === | ||
+ | |||
+ | ''Note: following code can be somehow optimised by adding small 0.01s delay or something like that. If someone knows how to, please write it here.'' | ||
Implement the '''mouseDown:''' method: | Implement the '''mouseDown:''' method: |
Revision as of 21:18, 16 February 2005
NSView is ...
Code chunks
Dragging objects
Note: following code can be somehow optimised by adding small 0.01s delay or something like that. If someone knows how to, please write it here.
Implement the mouseDown: method:
- (void)mouseDown:(NSEvent *)event { NSPoint oldPoint; NSEvent *presentEvent; NSEventType eventType = [event type]; unsigned int eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask; NSPoint point; float dx,dy; NSPoint location; point = [self convertPoint:[event locationInWindow] fromView:nil]; oldPoint = point; do { while (event && eventType != NSLeftMouseUp) { presentEvent = event; event = [NSApp nextEventMatchingMask: eventMask untilDate: [NSDate distantPast] inMode: NSEventTrackingRunLoopMode dequeue: YES]; eventType = [event type]; } point = [self convertPoint: [presentEvent locationInWindow] fromView: nil]; dx = point.x - oldPoint.x; dy = point.y - oldPoint.y; [self scrollRectToVisible:frame];
Add code here:
// // here move the dragged thing by: dx, dy // ....
Then finish the loop:
[self setNeedsDisplay:YES]; oldPoint = point; if (eventType == NSLeftMouseUp) { break; } event = [NSApp nextEventMatchingMask: eventMask untilDate: nil inMode: NSEventTrackingRunLoopMode dequeue: YES]; eventType = [event type]; } while (eventType != NSLeftMouseUp); [self setNeedsDisplay:YES]; }