diff --git a/AsyncDisplayKit/ASViewController.h b/AsyncDisplayKit/ASViewController.h index 170de10c8f..a16932ccf1 100644 --- a/AsyncDisplayKit/ASViewController.h +++ b/AsyncDisplayKit/ASViewController.h @@ -64,7 +64,12 @@ typedef ASTraitCollection * _Nonnull (^ASDisplayTraitsForTraitWindowSizeBlock)(C @interface ASViewController (ASRangeControllerUpdateRangeProtocol) -/// Automatically adjust range mode based on view events if the containing node confirms to the ASRangeControllerUpdateRangeProtocol +/** + * Automatically adjust range mode based on view events. If you set this to YES, the view controller or its node + * must conform to the ASRangeControllerUpdateRangeProtocol. + * + * Default value is NO. + */ @property (nonatomic, assign) BOOL automaticallyAdjustRangeModeBasedOnViewEvents; @end diff --git a/AsyncDisplayKit/ASViewController.mm b/AsyncDisplayKit/ASViewController.mm index bfdaefbe7c..82d8951175 100644 --- a/AsyncDisplayKit/ASViewController.mm +++ b/AsyncDisplayKit/ASViewController.mm @@ -27,6 +27,9 @@ BOOL _automaticallyAdjustRangeModeBasedOnViewEvents; BOOL _parentManagesVisibilityDepth; NSInteger _visibilityDepth; + BOOL _selfConformsToRangeModeProtocol; + BOOL _nodeConformsToRangeModeProtocol; + BOOL _didCheckRangeModeProtocolConformance; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil @@ -172,17 +175,29 @@ ASVisibilityDepthImplementation; - (void)setAutomaticallyAdjustRangeModeBasedOnViewEvents:(BOOL)automaticallyAdjustRangeModeBasedOnViewEvents { _automaticallyAdjustRangeModeBasedOnViewEvents = automaticallyAdjustRangeModeBasedOnViewEvents; + if (automaticallyAdjustRangeModeBasedOnViewEvents && !_didCheckRangeModeProtocolConformance) { + _selfConformsToRangeModeProtocol = [self conformsToProtocol:@protocol(ASRangeControllerUpdateRangeProtocol)]; + _nodeConformsToRangeModeProtocol = [_node conformsToProtocol:@protocol(ASRangeControllerUpdateRangeProtocol)]; + _didCheckRangeModeProtocolConformance = YES; + if (!_selfConformsToRangeModeProtocol && !_nodeConformsToRangeModeProtocol) { + NSLog(@"Warning: automaticallyAdjustRangeModeBasedOnViewEvents set to YES in %@, but range mode updating is not possible because neither view controller nor node %@ conform to ASRangeControllerUpdateRangeProtocol.", self, _node); + } + } } - (void)updateCurrentRangeModeWithModeIfPossible:(ASLayoutRangeMode)rangeMode { if (!_automaticallyAdjustRangeModeBasedOnViewEvents) { return; } - if (![_node conformsToProtocol:@protocol(ASRangeControllerUpdateRangeProtocol)]) { - return; + + if (_selfConformsToRangeModeProtocol) { + id rangeUpdater = (id)self; + [rangeUpdater updateCurrentRangeWithMode:rangeMode]; + } + + if (_nodeConformsToRangeModeProtocol) { + id rangeUpdater = (id)_node; + [rangeUpdater updateCurrentRangeWithMode:rangeMode]; } - - id updateRangeNode = (id)_node; - [updateRangeNode updateCurrentRangeWithMode:rangeMode]; } #pragma mark - Layout Helpers