A debug feature on ASControlNode to visualize tap-able areas (by adding a highlight overlay ASDisplayNode as a subnode) when addTarget:action:ControlEvent: is called. Uses hitTestSlop to get ASControlNode's UIEdgeInset and set the highlight overlay's frame to be the ASControlNode frame + edge insets. Disables clip to bounds on ASControlNode. Enabled by calling +[ASControlNode setEnableHitTestDebug:YES];

This commit is contained in:
Hannah Troisi
2016-03-05 16:08:55 -08:00
parent 30463db034
commit 9d5fed280d
3 changed files with 47 additions and 1 deletions

View File

@@ -7,6 +7,7 @@
*/
#import "ASControlNode.h"
#import "ASDisplayNode+Subclasses.h"
NS_ASSUME_NONNULL_BEGIN

View File

@@ -120,6 +120,11 @@ typedef NS_OPTIONS(NSUInteger, ASControlState) {
*/
- (void)sendActionsForControlEvents:(ASControlNodeEvent)controlEvents withEvent:(nullable UIEvent *)event;
/**
Class method to enable a visualization overlay of the tapable area on the ASControlNode. For app debugging purposes only.
@param enabled Specify YES to make this debug feature enabled when messaging the ASControlNode class.
*/
+ (void)setEnableHitTestDebug:(BOOL)enable;
@end
NS_ASSUME_NONNULL_END

View File

@@ -69,8 +69,12 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
@end
#pragma mark -
static BOOL _enableHitTestDebug = NO;
@implementation ASControlNode
{
ASDisplayNode *_debugHighlightOverlay;
}
#pragma mark - Lifecycle
- (id)init
@@ -85,6 +89,8 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
return self;
}
#pragma mark - ASDisplayNode Overrides
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
@@ -228,6 +234,18 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
if (!_controlEventDispatchTable) {
_controlEventDispatchTable = [[NSMutableDictionary alloc] initWithCapacity:kASControlNodeEventDispatchTableInitialCapacity]; // enough to handle common types without re-hashing the dictionary when adding entries.
// only show tap-able areas for views with 1 or more addTarget:action: pairs
if (_enableHitTestDebug) {
// add a highlight overlay node with area of ASControlNode + UIEdgeInsets
self.clipsToBounds = NO;
_debugHighlightOverlay = [[ASDisplayNode alloc] init];
_debugHighlightOverlay.layerBacked = YES;
_debugHighlightOverlay.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5];
[self addSubnode:_debugHighlightOverlay];
}
}
// Enumerate the events in the mask, adding the target-action pair for each control event included in controlEventMask
@@ -425,4 +443,26 @@ void _ASEnumerateControlEventsIncludedInMaskWithBlock(ASControlNodeEvent mask, v
{
}
#pragma mark - Debug
// Layout method required when _enableHitTestDebug is enabled.
- (void)layout
{
[super layout];
if (_debugHighlightOverlay) {
UIEdgeInsets insets = [self hitTestSlop];
CGRect controlNodeRect = self.bounds;
_debugHighlightOverlay.frame = CGRectMake(controlNodeRect.origin.x + insets.left,
controlNodeRect.origin.y + insets.top,
controlNodeRect.size.width - insets.left - insets.right,
controlNodeRect.size.height - insets.top - insets.bottom);
}
}
+ (void)setEnableHitTestDebug:(BOOL)enable
{
_enableHitTestDebug = enable;
}
@end