• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Live dragging - Need Help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Live dragging - Need Help


  • Subject: Live dragging - Need Help
  • From: "" <email@hidden>
  • Date: Wed, 1 Mar 2006 16:11:54 -0500 (EST)

Hello to everybody.

I'm trying to create a little app as a demo with the Cocoa framework that uses a view where there is a rectangle. When the user clicks the view the app checks the - mouseDown:(NSEvent *) to see if the mouse is in the rectangle. If so it stores the coordinates of the mouse. Then, when the - mouseDragged:(NSEvent *) is called, the app updates the coordinates of the origin of the rectangle in order to move it. But it is TERRIBLY SLOW! The rectangle arrives after a while at the point where the mouse is.
I've already tried to change [self setNeedsDisplay:YES] to [self display] but with no success.

Please, I need a suggestion!!! Could someone send me a code example?

Thanks a lot in advance.

Here it is my code fragment:

@implementation MainView

float dx, dy;
BOOL dragging;
NSPoint orgPoint;

- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		// Add initialization code here
        _frame1 = NSMakeRect(10,10,FRAME_W,FRAME_H);   // Build the frame of the rectangle
        dragging = NO;   // Initialize the dragging flag to NO
	}
	return self;
}

- (void)drawRect:(NSRect)rect
{
    NSEraseRect(rect);

    // Draw the rectangle
    if (NSIntersectsRect(rect, _frame1)) {
        [[NSColor yellowColor] set];
        NSRectFill(_frame1);

        [[NSColor blackColor] set];
        NSFrameRect(_frame1);
    }
}

- (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;
    }
}


_______________________________________________
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:

This email sent to email@hidden

  • Prev by Date: Removing NSLinkAttributeName doesn't remove the other attributes
  • Next by Date: Re: objc_msgsend intel and nil objects
  • Previous by thread: Re: Removing NSLinkAttributeName doesn't remove the other attributes
  • Next by thread: NSTextField Masking
  • Index(es):
    • Date
    • Thread