Question

how to move subviews inside a super view (using osx 10.6) using mouse?
i have programmatically created five NSimageView's as subviews using for loop.. how to select and drag each imageview

Was it helpful?

Solution 2

finally i got the answer

    - (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (NSView *)hitTest:(NSPoint)aPoint
{
    NSEnumerator *subviews = [[self subviews] objectEnumerator]  ;
    NSView  *hitView    ;
    NSLog(@"hit");
    fHitView = nil  ;
    while (hitView = [subviews nextObject]) {
        NSRect  frame = [hitView frame] ;       

        if (NSPointInRect(aPoint,frame))
            if ([hitView isKindOfClass:[DraggableView class]] && ![(DraggableView *)hitView dragEnabled]) {
                return hitView   ;
            }
            else    {
                fHitView = hitView  ;
                fHitPoint = aPoint   ;
                fFrameWhenHit = [hitView frame]   ;
                return self   ;
            }
    }

    return nil  ;
}


- (void)mouseDragged:(NSEvent *)theEvent
{
    if (fHitView != nil)    {
        NSPoint locationInWindow = [theEvent locationInWindow]  ;
        NSPoint locationInMySelf = [self convertPoint:locationInWindow fromView:[[self window] contentView]]    ;

        [fHitView setFrame:NSOffsetRect(fFrameWhenHit,locationInMySelf.x - fHitPoint.x, locationInMySelf.y - fHitPoint.y)]  ;
        [self setNeedsDisplay:YES]  ;
    }
}

insert into subclass of NSview and change the class name of NScustomView class as subclass name...

OTHER TIPS

In a nutshell, you'd want to make your subviews drag sources, and make your target view a destination. This means implementing something like the NSDraggingSource and NSDraggingDestination protocols.

Take a look at Apple's Drag & Drop documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DragandDrop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top