Dragging CALayer in an event loop
Dragging CALayer in an event loop
- Subject: Dragging CALayer in an event loop
- From: Graham Cox <email@hidden>
- Date: Mon, 17 Dec 2012 21:15:54 +1100
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