2011-07-06 7 views
1

나는 draghow를 구현하고 있습니다. customView에 대해 드롭 &; 이 customView는 NSView의 하위 클래스이며 일부 요소를 포함합니다. 드래그 작업을 시작할 때 dragImage는 사용자 정의보기와 동일한 크기의 직사각형 회색 상자입니다.드래그 이미지의 드래그 앤 드롭 생성

이 내가 쓴 코드입니다 :

-(void) mouseDragged:(NSEvent *)theEvent 
{ 
    NSPoint downWinLocation = [mouseDownEvent locationInWindow]; 
    NSPoint dragWinLocation = [theEvent locationInWindow]; 

    float distance = hypotf(downWinLocation.x - dragWinLocation.x, downWinLocation.y - downWinLocation.x); 
    if (distance < 3) { 
     return; 
    } 

    NSImage *viewImage = [self getSnapshotOfView]; 
    NSSize viewImageSize = [viewImage size]; 
    //Get Location of mouseDown event 
    NSPoint p = [self convertPoint:downWinLocation fromView:nil]; 

    //Drag from the center of image 
    p.x = p.x - viewImageSize.width/2; 
    p.y = p.y - viewImageSize.height/2; 

    //Write on PasteBoard 
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:NSDragPboard]; 
    [pb declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] 
       owner:nil]; 
    //Assume fileList is list of files been readed 
    NSArray *fileList = [NSArray arrayWithObjects:@"/tmp/ciao.txt", @"/tmp/ciao2.txt", nil]; 
    [pb setPropertyList:fileList forType:NSFilenamesPboardType]; 

    [self dragImage:viewImage at:p offset:NSMakeSize(0, 0) event:mouseDownEvent pasteboard:pb source:self slideBack:YES]; 
} 

는 그리고 이것은 내가 스냅 샷을 만드는 데 사용하는 기능입니다 : 이것은 내있는 CustomView에 드래그 조작의 이미지가합니다 (

- (NSImage *) getSnapshotOfView 
{ 
    NSRect rect = [self bounds] ; 

    NSImage *image = [[[NSImage alloc] initWithSize: rect.size] autorelease]; 

    NSRect imageBounds; 
    imageBounds.origin = NSZeroPoint; 
    imageBounds.size = rect.size; 

    [self lockFocus]; 
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageBounds]; 
    [self unlockFocus]; 

    [image addRepresentation:rep]; 
    [rep release]; 

    return image; 
} 

입니다 아이콘이 있고 레이블이 "drag me") DragDrop

왜 내 드래그 이미지는 회색 상자입니까?

+0

'-getSnapshotOfView'에 메모리 누수가 있습니다. 반환 된 이미지를'autorelease' 할 필요가 있습니다. 또한'imageBounds' 변수를 사용하지 마십시오. 그러나 이러한 문제는 문제의 원인이 아닙니다. –

+0

사용자 정의보기의 드로잉 코드를 게시 할 수 있습니까? –

+0

방금 ​​코드를 수정했습니다. 내 customView에 대한 드로잉 코드가 없습니다. IB를 사용하여 삽입 한 장식 요소가 두 개 있습니다. 뷰의 이름이 "드래그 가능한보기" [인터페이스 빌더 이미지 (http://cl.ly/3C3B1c2B2J3F082b3H0T/Captura_de_pantalla_2011-07-07_a_las_09.37.41.png) – Giuseppe

답변

4

귀하의 의견에 IB의 스크린 샷에서 볼 때 레이어가 뒷받침 된 것처럼 보입니다. 레이어 지원 뷰는 일반 윈도우 백업 저장소와 별도로 자체 그래픽 영역에 그립니다.

이 코드 :

[self lockFocus]; 
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageBounds]; 
[self unlockFocus]; 

효과적으로 윈도우 배킹 스토어로부터 픽셀을 판독한다. 보기가 레이어 백업이므로 내용이 선택되지 않습니다.

레이어 기반보기없이 시도해보십시오.

+0

완벽한 해답 이미지 당신이 할 수있는! – Giuseppe