代码遍历当前window获取视图层级结构

一,选择捕获视图层级的时机;



我选择在触摸到屏幕的时候捕获;


二,开始遍历;



- (NSMutableArray *)pageStructure:(UIView *)sView
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (UIView *view in sView.subviews) {

        if (view.subviews.count) {
            NSMutableArray *array = [self pageStructure:view];
            
            //这里的key是想要获取的view的信息;
            NSDictionary *dict = @{view: array};
            [arr addObject:dict];
        } else {
            
            //这里的元素是想要获取的view的信息;
            [arr addObject:[view copy]];
        }

    }
    return arr;
}


三,得到数据;



以这个界面为例;

{
  "UIWindow, {{0, 0}, {375, 667}}" : [
    {
      "UILayoutContainerView, {{0, 0}, {375, 667}}" : [
        {
          "UITransitionView, {{0, 0}, {375, 667}}" : [
            {
              "UIViewControllerWrapperView, {{0, 0}, {375, 667}}" : [
                {
                  "UILayoutContainerView, {{0, 0}, {375, 667}}" : [
                    {
                      "UINavigationTransitionView, {{0, 0}, {375, 667}}" : [
                        {
                          "UIViewControllerWrapperView, {{0, 0}, {375, 667}}" : [
                            {
                              "UIView, {{0, 0}, {375, 667}}" : [
                                {
                                  "UITableView, {{0, 0}, {80.5, 667}}" : [
                                    {
                                      "UITableViewWrapperView, {{0, 0}, {80.5, 603}}" : [
                                        {
                                          "GCClassifyListTableViewCell, {{0, 572}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称13",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 528}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称12",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 484}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称11",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 440}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称10",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 396}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称9",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 352}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称8",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 308}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称7",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 264}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称6",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 220}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称5",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 176}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称4",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 132}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称3",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 88}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称2",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 44}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称1",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        },
                                        {
                                          "GCClassifyListTableViewCell, {{0, 0}, {80.5, 44}}" : [
                                            "UITableViewCellContentView, {{0, 0}, {80.5, 44}}",
                                            "UILabel, {{10, 0}, {68, 50}}, text:商品名称0",
                                            "UIView, {{0, 10}, {4, 30}}"
                                          ]
                                        }
                                      ]
                                    },
                                    "UIView, {{0, 1320}, {80.5, 0}}",
                                    "UIImageView, {{75, 593}, {2.5, 7}}",
                                    "UIImageView, {{70.5, 597.5}, {7, 2.5}}"
                                  ]
                                },
                                {
                                  "UICollectionView, {{81.5, 64}, {293.5, 603}}" : [
                                    {
                                      "GCClassifyDetailHeaderCollectionViewCell, {{0, 0}, {293.5, 129}}" : [
                                        "UIView, {{0, 0}, {293.5, 129}}",
                                        "UIImageView, {{13.5, 20}, {266, 100}}"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{0, 130}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称0"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{97.833333333333343, 130}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称1"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{195.66666666666666, 130}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称2"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{0, 260}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称3"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{97.833333333333343, 260}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称4"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{195.66666666666666, 260}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称5"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{0, 390}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称6"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{97.833333333333343, 390}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称7"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{195.66666666666666, 390}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称8"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{0, 520}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称9"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{97.833333333333343, 520}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称10"
                                      ]
                                    },
                                    {
                                      "GCClassifyDetailCollectionViewCell, {{195.66666666666666, 520}, {97.833333333333329, 130}}" : [
                                        "UIView, {{0, 0}, {97.833333333333329, 130}}",
                                        "UIImageView, {{14.916666666666664, 14}, {68, 48}}",
                                        "UILabel, {{0, 72}, {97.833333333333329, 30}}, text:商品名称11"
                                      ]
                                    },
                                    "UIImageView, {{288, 3}, {2.5, 251.5}}",
                                    "UIImageView, {{283.5, 597.5}, {7, 2.5}}"
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "UINavigationBar, {{0, 20}, {375, 44}}" : [
                        {
                          "_UIBarBackground, {{0, -20}, {375, 64}}" : [
                            "UIImageView, {{0, 64}, {375, 0.5}}",
                            {
                              "UIVisualEffectView, {{0, 0}, {375, 64}}" : [
                                "_UIVisualEffectBackdropView, {{0, 0}, {375, 64}}",
                                "_UIVisualEffectFilterView, {{0, 0}, {375, 64}}"
                              ]
                            }
                          ]
                        },
                        {
                          "UIButton, {{71, 6}, {296, 32}}" : [
                            "UIButtonLabel, {{0, 7.5}, {113.5, 17}}, text: ? 推荐搜索内容"
                          ]
                        },
                        {
                          "UINavigationItemButtonView, {{8, 6}, {57, 30}}" : [
                            "UILabel, {{19, 5.5}, {38, 21.5}}, text:Back"
                          ]
                        },
                        "_UINavigationBarBackIndicatorView, {{8, 11.5}, {13, 21}}"
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          "UITabBar, {{0, 618}, {375, 49}}" : [
            {
              "_UIBarBackground, {{0, 0}, {375, 49}}" : [
                "UIImageView, {{0, -0.5}, {375, 0.5}}",
                {
                  "UIVisualEffectView, {{0, 0}, {375, 49}}" : [
                    "_UIVisualEffectBackdropView, {{0, 0}, {375, 49}}",
                    "_UIVisualEffectFilterView, {{0, 0}, {375, 49}}"
                  ]
                }
              ]
            },
            {
              "UITabBarButton, {{2, 1}, {90, 48}}" : [
                "UITabBarButtonLabel, {{42.5, 35}, {5, 12}}, text:1"
              ]
            },
            {
              "UITabBarButton, {{96, 1}, {90, 48}}" : [
                "UITabBarButtonLabel, {{41.5, 35}, {6.5, 12}}, text:2"
              ]
            },
            {
              "UITabBarButton, {{190, 1}, {89, 48}}" : [
                "UITabBarButtonLabel, {{41, 35}, {6.5, 12}}, text:3"
              ]
            },
            {
              "UITabBarButton, {{283, 1}, {90, 48}}" : [
                "UITabBarButtonLabel, {{34.5, 35}, {20.5, 12}}, text:商城"
              ]
            }
          ]
        }
      ]
    }
  ]
}
页面结构数据;

在json视图中打开;


附:在获取页面结构的方法里view作为Dictionary的key和Array的元素,这里重写了view的copy方法(开发时勿用);

- (id)copyWithZone:(NSZone *)zone
{

    if ([self isKindOfClass:[UILabel class]]) {
        return [NSString stringWithFormat:@"%@, %@, text:%@", NSStringFromClass([self class]), NSStringFromCGRect(self.frame), ((UILabel *)self).text];
        
    } else {
        return [NSString stringWithFormat:@"%@, %@", NSStringFromClass([self class]), NSStringFromCGRect(self.frame)];
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值