Re: Dragging CALayer in an event loop
Re: Dragging CALayer in an event loop
- Subject: Re: Dragging CALayer in an event loop
- From: Kyle Sluder <email@hidden>
- Date: Mon, 17 Dec 2012 09:25:47 -0800
On Dec 17, 2012, at 2:15 AM, 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];
What if you call +flush before committing the transaction? CATransaction is dependent on the runloop to close its enclosing implicit transactions. This might not happen in the event-tracking runloop mode.
(iI only Apple would document what runloop mode(s) CA relies on…)
--Kyle Sluder
_______________________________________________
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