Swiftgram/examples/LayoutSpecPlayground/Sample/PlaygroundContainerNode.m
appleguy 55b5dff80c [ASLayoutSpec] Initial commit to support visualizing layout specs (with Playground app). (#2554)
* Initial ASLayoutSpecPlayground commit

* Initial exploratory stab at the main challenge of the app - visualizing ASLayoutSpecs

* Halfway through moving debug features out of ASDK framework files and into debug files. Project builds.

* [ASLayoutSpecPlayground] Created new Inspector node, cleaning up internal implementation to start formalizing support for layout spec visualization.

* Workaround for ensuring creation of visualizerNode for ALL layoutspecs

* continued development

* Layout Inspector Work in Progress

* Resizing the playground works in the shrink direction, not for grow.

* added new ASLayoutableInspectorNode features

* Cleaned up examples code.

* Cleaning up  code.

* more code cleanup

* [ASLayoutableInspector] Transition to an ASTableNode-based architecture to support larger numbers of buttons / customizable types.

* [ASLayoutableInspector] Support different layoutable property types to set up buttons that can edit all of them.

* Huy debugging

* Refactored layout inspector code for extensibility.

* Properly lock layoutableContextMap

* Fix context handling in ASDisplayNode:measureWithSizeRange

* Fix ASLayoutSpecPlayground:ViewController:toggleVisualization

* added slider to InspectorCell

* [ASLayoutSpecPlayground] Improvements to propagation of visualize mode, resize handle, minor cleanup.

* Fix to ASEnvironment

* [ASLayoutSpecPlayground] Fix a few minor issues from the merge with latest master.

* Implement layout spec cache

* add pager ndoe

* add more examples

* add more layout examples

* [ASLayoutPlayground] Fix merge issues

* [ASLayoutPlayground] Fix up the example project from the 2.0 API changes.

* [ASLayoutPlayground] Some fixes (#2411)

* [ASLayoutPlayground]: Some fixes
* Fixed crash when tapping descender.
* Fixed setting the item to inspect.
* Fixed button states in inspector node.
* Added sliders for spacingBefore, spacingAfter, ascender.

* [ASLayoutSpecPlayground] Deselect the buttons when editing is over.

* [ASLayoutSpecPlayground] Changed flexGrow/Shrink's values from YES/NO to 1.0/0.0

* [Project] Create new Debug/ directory for advanced tools dedicated to debugging.

* [LayoutSpecPlayground] Rename project without AS in title, to be consistent with LayoutSpecExamples.

* [Bulid] Fix Xcode project to use new Debug subdirectory / group.

* [Bulid] Fix a small merge error.

* [Build] Fix build issue for Framework target.

* [Bulid] Fix podspec to expose InspectorNode header; Remove old-cocoapods emojis from ASDKgram :)

* Move aside ASLayoutSpecPlayground-Swift to match master

* [LayoutSpecPlayground] Cleanup implementation in several files, xcodeproj, etc.

* [ASControlNode] Add comment for new assertion, to be enabled in a separate diff.
2016-11-08 20:16:16 -08:00

125 lines
4.0 KiB
Objective-C

//
// PlaygroundContainerNode.m
// Sample
//
// Created by Hannah Troisi on 3/19/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import "PlaygroundContainerNode.h"
#import "LayoutExampleNodes.h"
#import "PhotoPostNode.h"
#import <AsyncDisplayKit/ASLayoutElementInspectorNode.h>
#import <AsyncDisplayKit/AsyncDisplayKit+Debug.h>
#define RESIZE_HANDLE_SIZE 30
@implementation PlaygroundContainerNode
{
ASDisplayNode *_playgroundNode;
ASImageNode *_resizeHandle;
CGPoint _resizeStartLocation;
}
#pragma mark - Lifecycle
+ (NSUInteger)containerNodeCount
{
return 5;
}
+ (ASDisplayNode *)nodeForIndex:(NSUInteger)index
{
switch (index) {
case 0: return [[HorizontalStackWithSpacer alloc] init];
case 1: return [[PhotoWithInsetTextOverlay alloc] init];
case 2: return [[PhotoWithOutsetIconOverlay alloc] init];
case 3: return [[FlexibleSeparatorSurroundingContent alloc] init];
case 4: return [[PhotoPostNode alloc] initWithIndex:0];
default: return [[PhotoPostNode alloc] initWithIndex:1];
}
}
- (instancetype)initWithIndex:(NSUInteger)index
{
self = [super init];
if (self) {
self.backgroundColor = [UIColor whiteColor]; //[UIColor colorWithRed:255/255.0 green:181/255.0 blue:68/255.0 alpha:1];
self.automaticallyManagesSubnodes = YES;
_playgroundNode = [[self class] nodeForIndex:index];
_resizeHandle = [[ASImageNode alloc] init];
_resizeHandle.image = [UIImage imageNamed:@"resizeHandle"];
_resizeHandle.userInteractionEnabled = YES;
// [self addSubnode:_resizeHandle];
[ASLayoutElementInspectorNode sharedInstance].style.flexBasis = ASDimensionMakeWithFraction(1.0);
[ASLayoutElementInspectorNode sharedInstance].vizNodeInsetSize = 10.0;
self.shouldVisualizeLayoutSpecs = NO;
self.shouldCacheLayoutSpec = NO;
}
return self;
}
- (void)didLoad
{
[super didLoad];
UIPanGestureRecognizer *gr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(resizePlayground:)];
[_resizeHandle.view addGestureRecognizer:gr];
}
// manually layout _resizeHandle // FIXME: add this to an overlayStack in layoutSpecThatFits?
- (void)layout
{
[super layout];
[self.view bringSubviewToFront:_resizeHandle.view];
CGSize playgroundSize = _playgroundNode.calculatedLayout.size;
CGRect rect = CGRectZero;
rect.size = CGSizeMake(RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE);
rect.origin = CGPointMake(playgroundSize.width - rect.size.width, playgroundSize.height - rect.size.height);
_resizeHandle.frame = rect;
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
_playgroundNode.style.flexGrow = 1.0;
_playgroundNode.style.flexShrink = 1.0;
return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)
child:_playgroundNode];
}
#pragma mark - Gesture Handling
- (void)resizePlayground:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
_resizeStartLocation = [sender locationInView:sender.view];
}
else if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint location = [sender locationInView:sender.view];
CGPoint translation = CGPointMake(location.x - _resizeStartLocation.x, location.y - _resizeStartLocation.y);
[self changePlaygroundFrameWithTranslation:translation];
}
else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed) {
_resizeStartLocation = CGPointZero;
}
}
- (void)changePlaygroundFrameWithTranslation:(CGPoint)translation
{
ASSizeRange constrainedSize = self.constrainedSizeForCalculatedLayout;
constrainedSize.max.width = MAX(0, constrainedSize.max.width + translation.x);
constrainedSize.max.height = MAX(0, constrainedSize.max.height + translation.y);
[self.delegate relayoutWithSize:constrainedSize];
}
@end