Dragable CALayer

Any way to make CALayer a draggable user? If so, how?

(In Cocoa - Mac)

+3
source share
2 answers

Layers cannot receive mouse events themselves. You will need to handle events in the view or view controller containing this layer.

If the event mouseDragged:occurs at a level (see -[CALayer hitTest:]and -[CALayer containsPoint:]to check this), adjust the layer positionaccordingly. You probably want to turn off implicit animations to immediately follow the mouse pointer (instead of lagging a bit behind the position property animation):

[CATransaction begin];
[CATransaction setDisableActions:YES];
layer.position = ...;
[CATransaction commit];
+4
source

CALayer, , .

NSRect rect = NSZeroRect;
    rect.size = NSMakeSize( SSRandomFloatBetween( 300.0, 200.0 ), SSRandomFloatBetween( 300.0, 200.0 ));

    NSWindow *newWin = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSWindowBackingLocationDefault defer:YES];
    [newWin setBackgroundColor: [NSColor clearColor]];
    [newWin setOpaque:NO];
    [newWin setIgnoresMouseEvents:NO];
    [newWin setMovableByWindowBackground:YES];
    [newWin makeKeyAndOrderFront:self];

    [[newWin contentView] setWantsLayer:YES];

    NSRect contentFrame = [[newWin contentView] frame];
    CALayer *newWinLayer = [CALayer layer];
    newWinLayer.frame = NSRectToCGRect(contentFrame);

    layer.backgroundColor=CGColorCreateGenericGray(0.0f, 0.5f);
    layer.borderColor=CGColorCreateGenericGray(0.756f, 0.5f);
    layer.borderWidth=5.0;

        // Calculate random origin point
    rect.origin = SSRandomPointForSizeWithinRect( rect.size, [window frame] );

        // Set the layer frame to our random rectangle.
    layer.frame = NSRectToCGRect(rect);
    layer.cornerRadius = 25.0f;
  [newWinLayer addSublayer:layer];

, ( ) , .

, CALayer ?

+1

Source: https://habr.com/ru/post/1794931/


All Articles