how to move subviews inside a super view

主宰稳场 提交于 2019-12-06 13:37:36

问题


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


回答1:


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




回答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...



来源:https://stackoverflow.com/questions/15788219/how-to-move-subviews-inside-a-super-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!