Re: Need HELP - Dragging a view
Re: Need HELP - Dragging a view
- Subject: Re: Need HELP - Dragging a view
- From: Keith Wilson <email@hidden>
- Date: Thu, 2 Mar 2006 21:04:27 +1100
Your basic problem is that you are redrawing the whole screen a
zillion times - depending on how fast the user moves the mouse. There
are many solutions but since you appear to be a relative newbie,
here is a simple solution that captures a bitmap of the screen and
simply redraws the rectangle on top during each invocation of drawRect:
- mouseDown - do nothing in here about dragging
-(void)mouseDragged:(NSEvent*)theEvent
{
NSPoint pt = [self convertPoint:[theEvent locationInWindow]
fromView:nil];//locationInWindow is relative to top left corner of
screen
... decide what is being dragged
...
[self trackMouseButton];
}
// ********************************************
and in trackMouseButton hang onto the events until the user lets go
of the left mouseButton
- something like
// ********************************************
-(void)trackMouseButton
{
//capture a bitmap of the current screen
if(myBitmap)
[myBitmap release];
[self lockFocus];
myBitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
[self bounds]];
[self unlockFocus];
//set some rects or points so drawRect: will know what to
superimpose on the bitmap each time it gets a needsDisplay:YES
origPoint = NSZeroPoint; //you will need origPoint if you are
drawing a rubberBand
currentPoint = NSZeroPoint;
targetRect = NSZeroRect;
//now we capture the events in a loop until the user lets go of the
mouse button
NSEvent *theEvent;
while(1)
{ theEvent = [[self window] nextEventMatchingMask:
(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
currentPoint = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
//in your case decide on what rectange needs to be drawn when the
cursor is at currentPoint - but
//do not draw it now
.....
targetRect = whatever ....
if ([theEvent type] == NSLeftMouseUp)
break;
[self setNeedsDisplay:YES];
}
/// do something to save the new position of targetRect
if(myBitmap)
[myBitmap release];
myBitmap = nil;
originalPoint = NSZeroPoint;
currentPoint = NSZeroPoint;
targetRect = NSZeroRect;
[self setNeedsDisplay:YES];
}
// ********************************************
and then in the drawRect
// ********************************************
-(void)drawRect:(NSRect)rect
{
[bgColour setFill];
NSRectFill([self bounds]);
if(myBitmap != nil)
{ [myBitmap drawInRect:rect]; //this displays the bitmap captured
when you started dragging
//draw the rectangle as per targetRect
....
return;
}
.. etc as per normal drawing of the screen when myBitmap is not nil
...
}
// ********************************************
On 02/03/2006, at 8:00 PM, "" <email@hidden>
<email@hidden> wrote:
Thanks for the answer.
I tried also to draw a rectangle and to drag it around (using
Cocoa). Same result: slow.
My code follows this kind of reasoning:
In -mouseDown:(NSEvent *)theEvent of the superview I check if the
mouse
click point lies in the rectangle. If so I store its coordinates.
In -mouseDragged:(NSEvent *)theEvent I offset the rectangle and I
refresh the
superview.
Here it is a code sample:
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location
fromView:nil];
if ([self mouse:local_point inRect:_frame1]) {
orgPoint = local_point;
dragging = YES;
}
}
// Mouse dragging.
- (void)mouseDragged:(NSEvent *)theEvent
{
if (dragging) {
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self
convertPoint:event_location fromView:nil];
_frame1 = NSOffsetRect(_frame1, local_point.x -
orgPoint.x, local_point.y - orgPoint.y);
orgPoint = local_point;
[self setNeedsDisplay:YES]; // Graphics update
}
}
// Set the dragging flag to NO: we are no more dragging!
- (void)mouseUp:(NSEvent *)theEvent
{
if (dragging) {
dragging = NO;
}
}
--- On Thu 03/02, Scott Anguish < email@hidden > wrote:
From: Scott Anguish [mailto: email@hidden]
To: email@hidden
Cc: email@hidden
Date: Thu, 2 Mar 2006 03:05:56 -0500
Subject: Re: Need HELP - Dragging a view
it's best not to rely on views as your application's child objects
like this. In fact the Cocoa performance docs recommend against
it explicitly.this is probably terribly slow because you're
dragging the same view that is getting the events..On Mar 2, 2006,
at 2:34 AM, "" wrote:>> Hello!>> I did a little app with a view
that holds a subview.> My problem is to drag that subview around in
its superview,> but it is SLOW!>> I tried also to draw both the
subview and the superview contents> using Quartz (in the -drawRect:
methods), but with no success.>> Any suggestion?>> Thanks for any
answer.> David.>> My code in the subview implementation file>> -
(void)mouseDown:(NSEvent *)theEvent> {> NSPoint
event_location = [theEvent locationInWindow];> NSPoint local_point
= [self convertPoint:event_location > fromView:nil];> > dx =
local_point.x;> dy = local_point.y;> }>> - (void)mouseDragged:
(NSEvent *)theEvent> {> MainView *mainView;> mainView = (MainView
*)[self
superview];> > NSPoint event_location = [theEvent
locationInWindow];> NSPoint local_point = [[self superview] >
convertPoint:event_location fromView:nil];> > NSPoint origin;>
origin.x = local_point.x - dx;> origin.y = local_point.y - dy;>
[self setFrameOrigin:origin];> > [self autoscroll:theEvent];> >
[mainView setNeedsDisplay:YES];> }>>>>
_______________________________________________> Join Excite! -
http://www.excite.com> The most personalized portal on the Web!>>>
_______________________________________________> Do not post admin
requests to the list. They will be ignored.> Cocoa-dev mailing
list (email@hidden)> Help/Unsubscribe/Update your
email@hidden>> This email sent to email@hidden
_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40bigpond.net.au
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden