나는 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")
왜 내 드래그 이미지는 회색 상자입니까?
'-getSnapshotOfView'에 메모리 누수가 있습니다. 반환 된 이미지를'autorelease' 할 필요가 있습니다. 또한'imageBounds' 변수를 사용하지 마십시오. 그러나 이러한 문제는 문제의 원인이 아닙니다. –
사용자 정의보기의 드로잉 코드를 게시 할 수 있습니까? –
방금 코드를 수정했습니다. 내 customView에 대한 드로잉 코드가 없습니다. IB를 사용하여 삽입 한 장식 요소가 두 개 있습니다. 뷰의 이름이 "드래그 가능한보기" [인터페이스 빌더 이미지 (http://cl.ly/3C3B1c2B2J3F082b3H0T/Captura_de_pantalla_2011-07-07_a_las_09.37.41.png) – Giuseppe