代码遍历当前window获取视图层级结构
2018-01-11 20:00
246 查看
一,选择捕获视图层级的时机;
我选择在触摸到屏幕的时候捕获;
二,开始遍历;
- (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}}", b7e3 "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)]; } }
相关文章推荐
- rcp(插件开发)通过代码获取当前选择的视图ID
- 遍历UIView子视图,找出按钮控件,如果点击在范围内则返回当前控件
- PHP获取当前日期所在星期(月份)的开始日期与结束日期(实现代码)
- IOS程序中代码获取当前设备电量
- [c#]一步一步开发自己的自动代码生成工具之一:获取sql2005的数据库表结构
- asp.net获取当前网址url的各种属性(文件名、参数、域名 等)的代码
- 获取当前的版本代码和版本名称
- 如何获取当前视图中成为第一响应者的组件,并取消注册
- JavaScript获取当前页面上的指定对象示例代码
- Java反射代码--01.通过类名称加载一个类并且获取当前类实例
- js获取当前页的URL与window.location.href简单方法
- 获取当前运行代码的类名,方法名,行号
- JS 日期操作代码,获取当前日期,加一天,减一天
- 获取系统当前日期,一定日期格式的代码
- android上运行C++代码中的 time(NULL)获取当前时间戳,为什么得到的结果不对
- asp获取当前网页地址的代码
- 用javascript/JS获取当前日期+星期的代码
- window.location 的几个用法(获取当前页面地址、页面跳转到指定链接、刷新页面)
- List<Map<String,Object>>使用Java代码遍历以获取String,Object的值 .
- 获取当前网址URL(PHP代码函数)