2012-04-01 3 views
1

사용자가 마우스로 위치를 이동할 수 있도록 프로그래밍 방식으로 NSView를 만들 수 있습니까? 보기에 할당 할 속성은 무엇입니까? 감사!Draggable NSView

newView = [helpWindow contentView]; 
    [contentView addSubview:newView]; 
    //add properties of newView to be able to respond to touch and can be draggable 

답변

2

불행히도 setMoveByWindowBackground와 같은 간단한 방법은 없습니다. 창과 함께 할 수있는 방법은 없습니다. mouseDown :, mouseDragged :, mouseUp :을 재정의하고 마우스 포인터의 위치에 따라 setFrameOrigin :을 사용해야합니다. 뷰를 처음 클릭 할 때 뷰가 점프되지 않도록하려면 뷰의 원점과 처음 클릭 할 때 뷰의 moue 포인터 사이의 간격을 고려해야합니다. 다음은 부모보기에서 "타일"을 이동하는 프로젝트에서 만든 예제입니다 (이 게임은 "Upwords"라는 게임의 컴퓨터 버전 용이며 3D 스크래블과 같습니다).

-(void)mouseDown:(NSEvent *) theEvent{ 
    self.mouseLoc = [theEvent locationInWindow]; 
    self.movingTile = [self hitTest:self.mouseLoc]; //returns the object clicked on 
    int tagID = self.movingTile.tag; 
    if (tagID > 0 && tagID < 8) { 
     [self.viewsList exchangeObjectAtIndex:[self.viewsList indexOfObject:self.movingTile] withObjectAtIndex: 20]; // 20 is the highest index in the array in this case 
     [self setSubviews:self.viewsList]; //Reorder's the subviews so the picked up tile always appears on top 
     self.hit = 1; 
     NSPoint cLoc = [self.movingTile convertPoint:self.mouseLoc fromView:nil]; 
     NSPoint loc = NSMakePoint(self.mouseLoc.x - cLoc.x, self.mouseLoc.y - cLoc.y); 
     [self.movingTile setFrameOrigin:loc]; 
     self.kX = cLoc.x; //this is the x offset between where the mouse was clicked and "movingTile's" x origin 
     self.kY = cLoc.y; //this is the y offset between where the mouse was clicked and "movingTile's" y origin 
    } 
} 

-(void)mouseDragged:(NSEvent *)theEvent { 
    if (self.hit == 1) { 
     self.mouseLoc = [theEvent locationInWindow]; 
     NSPoint newLoc = NSMakePoint(self.mouseLoc.x - self.kX, self.mouseLoc.y - self.kY); 
     [self.movingTile setFrameOrigin:newLoc]; 
    } 
} 

이 예는 가능한 한 더 복잡한 문제를 지적합니다. 보기를 이동하면 다른보기 아래로 이동하는 것처럼 보일 수 있으므로 이동보기가 상위보기의 하위보기의 가장보기를 만들기 때문에주의해야합니다 (viewsList는 self.subview에서 가져온 배열입니다)