ASScrollNode.mm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // ASScrollNode.m
  3. // AsyncDisplayKit
  4. //
  5. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  6. // This source code is licensed under the BSD-style license found in the
  7. // LICENSE file in the root directory of this source tree. An additional grant
  8. // of patent rights can be found in the PATENTS file in the same directory.
  9. //
  10. #import <AsyncDisplayKit/ASScrollNode.h>
  11. #import <AsyncDisplayKit/ASDisplayNodeExtras.h>
  12. #import <AsyncDisplayKit/ASDisplayNode+FrameworkPrivate.h>
  13. #import <AsyncDisplayKit/ASDisplayNode+FrameworkSubclasses.h>
  14. #import <AsyncDisplayKit/ASLayout.h>
  15. #import <AsyncDisplayKit/_ASDisplayLayer.h>
  16. @interface ASScrollView : UIScrollView
  17. @end
  18. @implementation ASScrollView
  19. // This special +layerClass allows ASScrollNode to get -layout calls from -layoutSublayers.
  20. + (Class)layerClass
  21. {
  22. return [_ASDisplayLayer class];
  23. }
  24. - (ASScrollNode *)scrollNode
  25. {
  26. return (ASScrollNode *)ASViewToDisplayNode(self);
  27. }
  28. #pragma mark - _ASDisplayView behavior substitutions
  29. // Need these to drive interfaceState so we know when we are visible, if not nested in another range-managing element.
  30. // Because our superclass is a true UIKit class, we cannot also subclass _ASDisplayView.
  31. - (void)willMoveToWindow:(UIWindow *)newWindow
  32. {
  33. ASDisplayNode *node = self.scrollNode; // Create strong reference to weak ivar.
  34. BOOL visible = (newWindow != nil);
  35. if (visible && !node.inHierarchy) {
  36. [node __enterHierarchy];
  37. }
  38. }
  39. - (void)didMoveToWindow
  40. {
  41. ASDisplayNode *node = self.scrollNode; // Create strong reference to weak ivar.
  42. BOOL visible = (self.window != nil);
  43. if (!visible && node.inHierarchy) {
  44. [node __exitHierarchy];
  45. }
  46. }
  47. @end
  48. @implementation ASScrollNode
  49. {
  50. ASScrollDirection _scrollableDirections;
  51. BOOL _automaticallyManagesContentSize;
  52. CGSize _contentCalculatedSizeFromLayout;
  53. }
  54. @dynamic view;
  55. - (instancetype)init
  56. {
  57. return [super initWithViewBlock:^UIView *{ return [[ASScrollView alloc] init]; }];
  58. }
  59. - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
  60. restrictedToSize:(ASLayoutElementSize)size
  61. relativeToParentSize:(CGSize)parentSize
  62. {
  63. ASDN::MutexLocker l(__instanceLock__); // Lock for using our instance variables.
  64. ASSizeRange contentConstrainedSize = constrainedSize;
  65. if (ASScrollDirectionContainsVerticalDirection(_scrollableDirections)) {
  66. contentConstrainedSize.max.height = CGFLOAT_MAX;
  67. }
  68. if (ASScrollDirectionContainsHorizontalDirection(_scrollableDirections)) {
  69. contentConstrainedSize.max.width = CGFLOAT_MAX;
  70. }
  71. ASLayout *layout = [super calculateLayoutThatFits:contentConstrainedSize
  72. restrictedToSize:size
  73. relativeToParentSize:parentSize];
  74. if (_automaticallyManagesContentSize) {
  75. // To understand this code, imagine we're containing a horizontal stack set within a vertical table node.
  76. // Our parentSize is fixed ~375pt width, but 0 - INF height. Our stack measures 1000pt width, 50pt height.
  77. // In this case, we want our scrollNode.bounds to be 375pt wide, and 50pt high. ContentSize 1000pt, 50pt.
  78. // We can achieve this behavior by: 1. Always set contentSize to layout.size. 2. Set bounds to parentSize,
  79. // unless one dimension is not defined, in which case adopt the contentSize for that dimension.
  80. _contentCalculatedSizeFromLayout = layout.size;
  81. CGSize selfSize = parentSize;
  82. if (ASPointsValidForLayout(selfSize.width) == NO) {
  83. selfSize.width = _contentCalculatedSizeFromLayout.width;
  84. }
  85. if (ASPointsValidForLayout(selfSize.height) == NO) {
  86. selfSize.height = _contentCalculatedSizeFromLayout.height;
  87. }
  88. // Don't provide a position, as that should be set by the parent.
  89. layout = [ASLayout layoutWithLayoutElement:self
  90. size:selfSize
  91. sublayouts:layout.sublayouts];
  92. }
  93. return layout;
  94. }
  95. - (void)layout
  96. {
  97. [super layout];
  98. ASDN::MutexLocker l(__instanceLock__); // Lock for using our two instance variables.
  99. if (_automaticallyManagesContentSize) {
  100. CGSize contentSize = _contentCalculatedSizeFromLayout;
  101. if (ASIsCGSizeValidForLayout(contentSize) == NO) {
  102. NSLog(@"%@ calculated a size in its layout spec that can't be applied to .contentSize: %@. Applying parentSize (scrollNode's bounds) instead: %@.", self, NSStringFromCGSize(contentSize), NSStringFromCGSize(self.calculatedSize));
  103. contentSize = self.calculatedSize;
  104. }
  105. self.view.contentSize = contentSize;
  106. }
  107. }
  108. - (BOOL)automaticallyManagesContentSize
  109. {
  110. ASDN::MutexLocker l(__instanceLock__);
  111. return _automaticallyManagesContentSize;
  112. }
  113. - (void)setAutomaticallyManagesContentSize:(BOOL)automaticallyManagesContentSize
  114. {
  115. ASDN::MutexLocker l(__instanceLock__);
  116. _automaticallyManagesContentSize = automaticallyManagesContentSize;
  117. if (_automaticallyManagesContentSize == YES
  118. && ASScrollDirectionContainsVerticalDirection(_scrollableDirections) == NO
  119. && ASScrollDirectionContainsHorizontalDirection(_scrollableDirections) == NO) {
  120. // Set the @default value, for more user-friendly behavior of the most
  121. // common use cases of .automaticallyManagesContentSize.
  122. _scrollableDirections = ASScrollDirectionVerticalDirections;
  123. }
  124. }
  125. - (ASScrollDirection)scrollableDirections
  126. {
  127. ASDN::MutexLocker l(__instanceLock__);
  128. return _scrollableDirections;
  129. }
  130. - (void)setScrollableDirections:(ASScrollDirection)scrollableDirections
  131. {
  132. ASDN::MutexLocker l(__instanceLock__);
  133. _scrollableDirections = scrollableDirections;
  134. }
  135. @end