2011-09-16 5 views
0

NSView에 10 개의 하위 뷰가있는 경우 하위 뷰 중 하나를 드래그하면 나머지 뷰 중 하나가 닫힌 뷰를 가장 쉽게 확인할 수있는 방법은 무엇입니까? 내 코드가 작동하지만 어떻게 든 바이올린을 미세 조정하려면 원숭이 렌치를 사용하고 있다고 느낍니다. 더 우아한 방법이 있습니까?Objective-C : 드래그 된 NSView에 가장 가까운 NSView 가져 오기

파단은 (그래서이 도면 포함)
서브 뷰 툴팁들은 페이지 번호는 "Page_ 번호"

- (void) mouseUp: (NSEvent*) e { 

    //set up a ridiclous # for current distance 
    float mtDistance = 12000000.0; 

    //get this page number 
    selfPageNum = [[[self toolTip] substringFromIndex:5] intValue]; 

    //set up pageViews array 
    pageViews = [[NSMutableArray alloc] init]; 

    //loop through the subviews 
    for (int i=0; i<[subviews count]; i++) { 

      //set up the view 
      thisView = [subviews objectAtIndex:i]; 

      //filter for view classes 
      NSString* thisViewClass = [NSString stringWithFormat:@"%@", [thisView className]]; 
      if ([thisViewClass isEqual:@"Page"]) { 

        //add to the pageViews array 
        [pageViews addObject:thisView]; 

        if (self != thisView) { 
          //get the view and self frame 
          NSRect movedViewFrame = [self frame]; 
          NSRect thisViewFrame = [thisView frame]; 

          //get the location area (x*y) 
          float movedViewLoc = movedViewFrame.origin.x * (movedViewFrame.origin.y * -1); 
          float thisViewLoc = thisViewFrame.origin.x * (thisViewFrame.origin.y * -1); 

          //get the difference between x locations 
          float mtDifference = movedViewLoc - thisViewLoc; 
          if (mtDifference < 0) { 
            mtDifference = mtDifference * -1; 
          } 

          if (mtDifference < mtDistance) { 
            mtDistance = mtDifference; 
            closesView = thisView; 
          } 
        } 

      }//end class check 
     }//end loop 

//....more stuff 

} 

답변

2

같은 형식으로 포함이보기의 상위 뷰의 파단의 배열 http://en.wikipedia.org/wiki/Distance

distance formula

sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2))) 

편집 : 당신은 단지 소를 찾을 필요가 있기 때문에 rtest 거리, 정확한 거리가 아니라면, 당신은 제곱근을 취할 필요가 없습니다. 통찰력을 주셔서 감사합니다 Abibern

pow((x2 - x1), 2) + pow((y2 - y1), 2) 
+1

거리에 대한 표준 답변입니다. 그러나 거리를 비교할 때 제곱근을 계산할 때 순환을 낭비 할 필요가 없습니다. d1 Abizern