所以爲了做到這一點,我建議在初始化時將標記設置爲視圖,以便稍後可以輕鬆找到它們。
這裏我們要將視圖y座標添加到字典中,並將該關鍵字作爲視圖標記。假設這些是你唯一的標籤子視圖。否則有一個系統省略標籤。
// Setting views and frames.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
for (UIView *view in self.view.subviews) {
if (view.tag) {
[dict setObject:[NSNumber numberWithFloat:view.frame.origin.y] forKey:[NSNumber numberWithInt:view.tag]];
}
}
遍歷字典並按降序插入y值。
for (NSNumber *keyNum in [dict allKeys]) {
float x = [[dict objectForKey:keyNum] floatValue];
int count = 0;
if (floatArray.count > 0) {
for (NSNumber *num in floatArray) {
float y = [num floatValue];
if (x < y) {
count++;
[floatArray insertObject:[NSNumber numberWithFloat:x] atIndex:count];
[tagArray insertObject:keyNum atIndex:count];
break;
}
}
}else{
[floatArray insertObject:[NSNumber numberWithFloat:x] atIndex:count];
[tagArray insertObject:keyNum atIndex:count];
}
}
找回使用他們的標籤和位置通過每一個迭代,並使用bringSubViewToFront方法的意見你的意見,這應該堆他們在正確的順序。
注意:這裏假定你沒有在你的視圖中需要在層次結構之上的其他子視圖,如果是的話,我會使用insertSubview:AtIndex:方法。
for (NSNumber *num in tagArray) {
UIView *view = (UIView *)[self.view viewWithTag:[num integerValue]];
NSLog(@"view.frame.origin.y: %.2f",view.frame.origin.y);
[self.view bringSubviewToFront:view];
}
我沒有使用這個確切的解決方案,而是通過數組排序循環和使這一subivew到前面的概念解決了這個問題對我來說。標記正確。謝謝。 – Joel