所以为了做到这一点,我建议在初始化时将标记设置为视图,以便稍后可以轻松找到它们。
这里我们要将视图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