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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to