0
如果我有一個包含10個子視圖的NSView,並且拖動其中一個子視圖,那麼確定剩下的哪些視圖關閉到拖動的視圖最簡單的方法是什麼?我的代碼正在工作,但我不知何故覺得我在用扳手來調整小提琴。有沒有更優雅的方式?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