| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // FLEXViewExplorerViewController.m
- // Flipboard
- //
- // Created by Ryan Olson on 6/11/14.
- // Copyright (c) 2014 Flipboard. All rights reserved.
- //
- #import "FLEXViewExplorerViewController.h"
- #import "FLEXRuntimeUtility.h"
- #import "FLEXUtility.h"
- #import "FLEXObjectExplorerFactory.h"
- #import "FLEXViewSnapshotViewController.h"
- #import "FLEXPropertyEditorViewController.h"
- typedef NS_ENUM(NSUInteger, FLEXViewExplorerRow) {
- FLEXViewExplorerRowViewController,
- FLEXViewExplorerRowFrame,
- FLEXViewExplorerRowPreview
- };
- @interface FLEXViewExplorerViewController ()
- // Don't clash with UIViewController's view property
- @property (nonatomic, readonly) UIView *viewToExplore;
- @end
- @implementation FLEXViewExplorerViewController
- - (UIView *)viewToExplore
- {
- return [self.object isKindOfClass:[UIView class]] ? self.object : nil;
- }
- #pragma mark - Superclass Overrides
- - (NSString *)customSectionTitle
- {
- return @"Shortcuts";
- }
- - (NSArray *)customSectionRowCookies
- {
- NSArray *rowCookies = @[@(FLEXViewExplorerRowPreview),
- @(FLEXViewExplorerRowFrame)];
-
- if ([FLEXUtility viewControllerForView:self.viewToExplore]) {
- rowCookies = [@[@(FLEXViewExplorerRowViewController)] arrayByAddingObjectsFromArray:rowCookies];
- }
-
- return rowCookies;
- }
- - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
- {
- NSString *title = nil;
- if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
- title = @"View Controller";
- } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
- title = @"@property CGRect frame";
- } else if ([rowCookie isEqual:@(FLEXViewExplorerRowPreview)]) {
- title = @"Image Preview";
- }
- return title;
- }
- - (NSString *)customSectionSubtitleForRowCookie:(id)rowCookie
- {
- NSString *subtitle = nil;
- if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
- subtitle = [FLEXRuntimeUtility descriptionForIvarOrPropertyValue:[FLEXUtility viewControllerForView:self.viewToExplore]];
- } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
- subtitle = [FLEXUtility stringForCGRect:self.viewToExplore.frame];
- }
- return subtitle;
- }
- - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
- {
- return YES;
- }
- - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
- {
- UIViewController *drillInViewController = nil;
- if ([rowCookie isEqual:@(FLEXViewExplorerRowViewController)]) {
- drillInViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:[FLEXUtility viewControllerForView:self.viewToExplore]];
- } else if ([rowCookie isEqual:@(FLEXViewExplorerRowFrame)]) {
- // A quirk of UIView: frame is not actually a property from the perspective of the runtime.
- // We add the property to the class at runtime if it hasn't been added yet.
- [FLEXRuntimeUtility addFramePropertyToUIViewIfNeeded];
- objc_property_t frameProperty = class_getProperty([UIView class], "frame");
- drillInViewController = [[FLEXPropertyEditorViewController alloc] initWithTarget:self.viewToExplore property:frameProperty];
- } else if ([rowCookie isEqual:@(FLEXViewExplorerRowPreview)]) {
- if (!CGRectIsEmpty(self.viewToExplore.bounds)) {
- CGSize viewSize = self.viewToExplore.bounds.size;
- UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0.0);
- if ([self.viewToExplore respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
- [self.viewToExplore drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:YES];
- } else {
- CGContextRef imageContext = UIGraphicsGetCurrentContext();
- [self.viewToExplore.layer renderInContext:imageContext];
- }
- UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- drillInViewController = [[FLEXViewSnapshotViewController alloc] initWithImage:previewImage];
- }
- }
- return drillInViewController;
- }
- @end
|