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