ASScrollNode.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "ASScrollNode.h"
  11. #import "ASDisplayNodeExtras.h"
  12. #import "ASDisplayNode+FrameworkPrivate.h"
  13. #import "ASDisplayNode+FrameworkSubclasses.h"
  14. #import "ASLayout.h"
  15. #import "_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. BOOL _automaticallyManagesContentSize;
  51. CGSize _contentCalculatedSizeFromLayout;
  52. }
  53. @dynamic view;
  54. - (instancetype)init
  55. {
  56. return [super initWithViewBlock:^UIView *{ return [[ASScrollView alloc] init]; }];
  57. }
  58. - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize
  59. restrictedToSize:(ASLayoutElementSize)size
  60. relativeToParentSize:(CGSize)parentSize
  61. {
  62. ASLayout *layout = [super calculateLayoutThatFits:constrainedSize
  63. restrictedToSize:size
  64. relativeToParentSize:parentSize];
  65. ASDN::MutexLocker l(__instanceLock__); // Lock for using our two instance variables.
  66. if (_automaticallyManagesContentSize) {
  67. // To understand this code, imagine we're containing a horizontal stack set within a vertical table node.
  68. // Our parentSize is fixed ~375pt width, but 0 - INF height. Our stack measures 1000pt width, 50pt height.
  69. // In this case, we want our scrollNode.bounds to be 375pt wide, and 50pt high. ContentSize 1000pt, 50pt.
  70. // We can achieve this behavior by: 1. Always set contentSize to layout.size. 2. Set bounds to parentSize,
  71. // unless one dimension is not defined, in which case adopt the contentSize for that dimension.
  72. _contentCalculatedSizeFromLayout = layout.size;
  73. CGSize selfSize = parentSize;
  74. if (ASPointsValidForLayout(selfSize.width) == NO) {
  75. selfSize.width = _contentCalculatedSizeFromLayout.width;
  76. }
  77. if (ASPointsValidForLayout(selfSize.height) == NO) {
  78. selfSize.height = _contentCalculatedSizeFromLayout.height;
  79. }
  80. // Don't provide a position, as that should be set by the parent.
  81. layout = [ASLayout layoutWithLayoutElement:self
  82. size:selfSize
  83. sublayouts:layout.sublayouts];
  84. }
  85. return layout;
  86. }
  87. - (void)layout
  88. {
  89. [super layout];
  90. ASDN::MutexLocker l(__instanceLock__); // Lock for using our two instance variables.
  91. if (_automaticallyManagesContentSize) {
  92. CGSize contentSize = _contentCalculatedSizeFromLayout;
  93. if (ASIsCGSizeValidForLayout(contentSize) == NO) {
  94. 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));
  95. contentSize = self.calculatedSize;
  96. }
  97. self.view.contentSize = contentSize;
  98. }
  99. }
  100. - (BOOL)automaticallyManagesContentSize
  101. {
  102. ASDN::MutexLocker l(__instanceLock__);
  103. return _automaticallyManagesContentSize;
  104. }
  105. - (void)setAutomaticallyManagesContentSize:(BOOL)automaticallyManagesContentSize
  106. {
  107. ASDN::MutexLocker l(__instanceLock__);
  108. _automaticallyManagesContentSize = automaticallyManagesContentSize;
  109. }
  110. @end