[SOLVED] Re: Dragging CALayer in an event loop
[SOLVED] Re: Dragging CALayer in an event loop
- Subject: [SOLVED] Re: Dragging CALayer in an event loop
- From: Graham Cox <email@hidden>
- Date: Mon, 17 Dec 2012 23:30:18 +1100
OK, this is a bit weird, but I found a simple solution. If I add NSOtherMouseDraggedMask to my list of masks, it works fine, even though in the body of the code I now only actually move the layers for the NSLeftMouseDragged event type! In addition, the docs for NSOtherMouseDraggedEvent suggest it applies only to a mouse with >2 buttons, for a button other than left or right. My mouse is a simple 2-button mouse, so that should not apply.
Lately, I've been finding all sorts of weird event handling oddities like this which suggests that there may be some strange bugs in the event queueing stuff. I had a huge hassle trying to get events in a sensible order when responding to trackpad events in a similar loop.
Any insight from somebody who knows more about this stuff would be appreciated - right now I'm finding there is far too much "magic" needed to make things work and none of it is adequately documented.
--Graham
On 17/12/2012, at 9:15 PM, Graham Cox <email@hidden> wrote:
> I have a very simple piece of code that doesn't behave how I expected it to.
>
> When I drag a CALayer, it does not follow the mouse but snaps to its final location after I exit the loop. I've tried forcing the view to redisplay within the loop, etc but nothing works. How can I do this?
>
>
>
>
> - (BOOL) dragItems:(NSSet*) items withEvent:(NSEvent*) event
> {
> // keeps control and moves the set of items as the mouse is dragged
>
> NSUInteger mask = NSLeftMouseDragged | NSLeftMouseUp | NSLeftMouseDown;
> BOOL done = NO;
> BOOL dragged = NO;
> NSPoint previous, local;
>
> previous = [event locationInWindow];
>
> while( !done )
> {
> event = [self.view.window nextEventMatchingMask:mask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES];
> [self.view.window discardEventsMatchingMask:NSAnyEventMask beforeEvent:event];
>
> local = [event locationInWindow];
>
> NSPoint delta = NSMakePoint( local.x - previous.x, local.y - previous.y );
> previous = local;
>
> // move the layers' positions by <delta>
>
> [CATransaction begin];
> [CATransaction setAnimationDuration:0];
>
> for( CALayer* layer in items )
> {
> CGPoint pos = layer.position;
>
> pos.x += delta.x;
> pos.y -= delta.y;
>
> layer.position = pos;
> }
>
> [CATransaction commit];
>
> if( event.type == NSLeftMouseUp )
> done = YES;
> else if ( event.type == NSLeftMouseDragged )
> dragged = YES;
> }
>
> return dragged;
> }
>
>
> --Graham
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden