FLEXViewExplorerViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // FLEXViewExplorerViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/11/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXViewExplorerViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXUtility.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXViewSnapshotViewController.h"
  13. #import "FLEXPropertyEditorViewController.h"
  14. typedef NS_ENUM(NSUInteger, FLEXViewExplorerRow) {
  15. FLEXViewExplorerRowViewController,
  16. FLEXViewExplorerRowFrame,
  17. FLEXViewExplorerRowPreview
  18. };
  19. @interface FLEXViewExplorerViewController ()
  20. // Don't clash with UIViewController's view property
  21. @property (nonatomic, readonly) UIView *viewToExplore;
  22. @end
  23. @implementation FLEXViewExplorerViewController
  24. - (UIView *)viewToExplore
  25. {
  26. return [self.object isKindOfClass:[UIView class]] ? self.object : nil;
  27. }
  28. #pragma mark - Superclass Overrides
  29. - (NSString *)customSectionTitle
  30. {
  31. return @"Shortcuts";
  32. }
  33. - (NSArray *)customSectionRowCookies
  34. {
  35. NSArray *rowCookies = @[@(FLEXViewExplorerRowPreview),
  36. @(FLEXViewExplorerRowFrame)];
  37. if ([FLEXUtility viewControllerForView:self.viewToExplore]) {
  38. rowCookies = [@[@(FLEXViewExplorerRowViewController)] arrayByAddingObjectsFromArray:rowCookies];
  39. }
  40. return rowCookies;
  41. }
  42. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  43. {
  44. NSString *title = nil;
  45. if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
  46. title = @"View Controller";
  47. } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
  48. title = @"@property CGRect frame";
  49. } else if ([rowCookie isEqual:@(FLEXViewExplorerRowPreview)]) {
  50. title = @"Image Preview";
  51. }
  52. return title;
  53. }
  54. - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
  55. {
  56. NSString *subtitle = nil;
  57. if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
  58. subtitle = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[FLEXUtility viewControllerForView:self.viewToExplore]];
  59. } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
  60. subtitle = [FLEXUtility stringForCGRect:self.viewToExplore.frame];
  61. }
  62. return subtitle;
  63. }
  64. - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
  65. {
  66. return YES;
  67. }
  68. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  69. {
  70. UIViewController *drillInViewController = nil;
  71. if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
  72. drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:[FLEXUtility viewControllerForView:self.viewToExplore]];
  73. } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
  74. // A quirk of UIView: frame is not actually a property from the perspective of the runtime.
  75. // We add the property to the class at runtime if it hasn't been added yet.
  76. [FLEXRuntimeUtility addFramePropertyToUIViewIfNeeded];
  77. objc_property_t frameProperty = class_getProperty([UIView class], "frame");
  78. drillInViewController = [[FLEXPropertyEditorViewController alloc] initWithTarget:self.viewToExplore property:frameProperty];
  79. } else if ([rowCookie isEqual:@(FLEXViewExplorerRowPreview)]) {
  80. if (!CGRectIsEmpty(self.viewToExplore.bounds)) {
  81. CGSize viewSize = self.viewToExplore.bounds.size;
  82. UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0.0);
  83. if ([self.viewToExplore respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
  84. [self.viewToExplore drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:YES];
  85. } else {
  86. CGContextRef imageContext = UIGraphicsGetCurrentContext();
  87. [self.viewToExplore.layer renderInContext:imageContext];
  88. }
  89. UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
  90. UIGraphicsEndImageContext();
  91. drillInViewController = [[FLEXViewSnapshotViewController alloc] initWithImage:previewImage];
  92. }
  93. }
  94. return drillInViewController;
  95. }
  96. @end