ASCellNode.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // ASCellNode.h
  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/ASDisplayNode.h>
  11. NS_ASSUME_NONNULL_BEGIN
  12. @class ASCellNode, ASTextNode;
  13. typedef NSUInteger ASCellNodeAnimation;
  14. typedef NS_ENUM(NSUInteger, ASCellNodeVisibilityEvent) {
  15. /**
  16. * Indicates a cell has just became visible
  17. */
  18. ASCellNodeVisibilityEventVisible,
  19. /**
  20. * Its position (determined by scrollView.contentOffset) has changed while at least 1px remains visible.
  21. * It is possible that 100% of the cell is visible both before and after and only its position has changed,
  22. * or that the position change has resulted in more or less of the cell being visible.
  23. * Use CGRectIntersect between cellFrame and scrollView.bounds to get this rectangle
  24. */
  25. ASCellNodeVisibilityEventVisibleRectChanged,
  26. /**
  27. * Indicates a cell is no longer visible
  28. */
  29. ASCellNodeVisibilityEventInvisible,
  30. /**
  31. * Indicates user has started dragging the visible cell
  32. */
  33. ASCellNodeVisibilityEventWillBeginDragging,
  34. /**
  35. * Indicates user has ended dragging the visible cell
  36. */
  37. ASCellNodeVisibilityEventDidEndDragging,
  38. };
  39. /**
  40. * Generic cell node. Subclass this instead of `ASDisplayNode` to use with `ASTableView` and `ASCollectionView`.
  41. * @note When a cell node is contained inside a collection view (or table view),
  42. * calling `-setNeedsLayout` will also notify the collection on the main thread
  43. * so that the collection can update its item layout if the cell's size changed.
  44. */
  45. @interface ASCellNode : ASDisplayNode
  46. /**
  47. * @abstract When enabled, ensures that the cell is completely displayed before allowed onscreen.
  48. *
  49. * @default NO
  50. * @discussion Normally, ASCellNodes are preloaded and have finished display before they are onscreen.
  51. * However, if the Table or Collection's rangeTuningParameters are set to small values (or 0),
  52. * or if the user is scrolling rapidly on a slow device, it is possible for a cell's display to
  53. * be incomplete when it becomes visible.
  54. *
  55. * In this case, normally placeholder states are shown and scrolling continues uninterrupted.
  56. * The finished, drawn content is then shown as soon as it is ready.
  57. *
  58. * With this property set to YES, the main thread will be blocked until display is complete for
  59. * the cell. This is more similar to UIKit, and in fact makes AsyncDisplayKit scrolling visually
  60. * indistinguishable from UIKit's, except being faster.
  61. *
  62. * Using this option does not eliminate all of the performance advantages of AsyncDisplayKit.
  63. * Normally, a cell has been preloading and is almost done when it reaches the screen, so the
  64. * blocking time is very short. If the rangeTuningParameters are set to 0, still this option
  65. * outperforms UIKit: while the main thread is waiting, subnode display executes concurrently.
  66. */
  67. @property (nonatomic, assign) BOOL neverShowPlaceholders;
  68. /*
  69. * The kind of supplementary element this node represents, if any.
  70. *
  71. * @return The supplementary element kind, or @c nil if this node does not represent a supplementary element.
  72. */
  73. @property (nonatomic, copy, readonly, nullable) NSString *supplementaryElementKind;
  74. /*
  75. * The layout attributes currently assigned to this node, if any.
  76. *
  77. * @discussion This property is useful because it is set before @c collectionView:willDisplayNode:forItemAtIndexPath:
  78. * is called, when the node is not yet in the hierarchy and its frame cannot be converted to/from other nodes. Instead
  79. * you can use the layout attributes object to learn where and how the cell will be displayed.
  80. */
  81. @property (nonatomic, strong, readonly, nullable) UICollectionViewLayoutAttributes *layoutAttributes;
  82. /**
  83. * A Boolean value that is synchronized with the underlying collection or tableView cell property.
  84. * Setting this value is equivalent to calling selectItem / deselectItem on the collection or table.
  85. */
  86. @property (nonatomic, assign, getter=isSelected) BOOL selected;
  87. /**
  88. * A Boolean value that is synchronized with the underlying collection or tableView cell property.
  89. * Setting this value is equivalent to calling highlightItem / unHighlightItem on the collection or table.
  90. */
  91. @property (nonatomic, assign, getter=isHighlighted) BOOL highlighted;
  92. /**
  93. * The current index path of this cell node, or @c nil if this node is
  94. * not a valid item inside a table node or collection node.
  95. *
  96. * @note This property must be accessed on the main thread.
  97. */
  98. @property (nonatomic, readonly, nullable) NSIndexPath *indexPath;
  99. /**
  100. * The backing view controller, or @c nil if the node wasn't initialized with backing view controller
  101. * @note This property must be accessed on the main thread.
  102. */
  103. @property (nonatomic, readonly, nullable) UIViewController *viewController;
  104. /**
  105. * The owning node (ASCollectionNode/ASTableNode) of this cell node, or @c nil if this node is
  106. * not a valid item inside a table node or collection node or if those nodes are nil.
  107. */
  108. @property (weak, nonatomic, readonly, nullable) ASDisplayNode *owningNode;
  109. /*
  110. * ASCellNode must forward touch events in order for UITableView and UICollectionView tap handling to work. Overriding
  111. * these methods (e.g. for highlighting) requires the super method be called.
  112. */
  113. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
  114. - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
  115. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
  116. - (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event ASDISPLAYNODE_REQUIRES_SUPER;
  117. /**
  118. * Called by the system when ASCellNode is used with an ASCollectionNode. It will not be called by ASTableNode.
  119. * When the UICollectionViewLayout object returns a new UICollectionViewLayoutAttributes object, the corresponding ASCellNode will be updated.
  120. * See UICollectionViewCell's applyLayoutAttributes: for a full description.
  121. */
  122. - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes;
  123. /**
  124. * @abstract Initializes a cell with a given view controller block.
  125. *
  126. * @param viewControllerBlock The block that will be used to create the backing view controller.
  127. * @param didLoadBlock The block that will be called after the view controller's view is loaded.
  128. *
  129. * @return An ASCellNode created using the root view of the view controller provided by the viewControllerBlock.
  130. * The view controller's root view is resized to match the calculated size produced during layout.
  131. *
  132. */
  133. - (instancetype)initWithViewControllerBlock:(ASDisplayNodeViewControllerBlock)viewControllerBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock;
  134. /**
  135. * @abstract Notifies the cell node of certain visibility events, such as changing visible rect.
  136. *
  137. * @warning In cases where an ASCellNode is used as a plain node – i.e. not returned from the
  138. * nodeBlockForItemAtIndexPath/nodeForItemAtIndexPath data source methods – this method will
  139. * deliver only the `Visible` and `Invisible` events, `scrollView` will be nil, and
  140. * `cellFrame` will be the zero rect.
  141. */
  142. - (void)cellNodeVisibilityEvent:(ASCellNodeVisibilityEvent)event inScrollView:(nullable UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame;
  143. #pragma mark - UITableViewCell specific passthrough properties
  144. /* @abstract The selection style when a tap on a cell occurs
  145. * @default UITableViewCellSelectionStyleDefault
  146. * ASTableView uses these properties when configuring UITableViewCells that host ASCellNodes.
  147. */
  148. @property (nonatomic) UITableViewCellSelectionStyle selectionStyle;
  149. /* @abstract The accessory type view on the right side of the cell. Please take care of your ASLayoutSpec so that doesn't overlay the accessoryView
  150. * @default UITableViewCellAccessoryNone
  151. * ASTableView uses these properties when configuring UITableViewCells that host ASCellNodes.
  152. */
  153. @property (nonatomic) UITableViewCellAccessoryType accessoryType;
  154. /* @abstract The seperator inset of the cell seperator line
  155. * ASTableView uses these properties when configuring UITableViewCells that host ASCellNodes.
  156. */
  157. @property (nonatomic) UIEdgeInsets seperatorInset;
  158. @end
  159. @interface ASCellNode (Unavailable)
  160. - (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable;
  161. - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable;
  162. - (void)setLayerBacked:(BOOL)layerBacked AS_UNAVAILABLE("ASCellNode does not support layer-backing");
  163. @end
  164. /**
  165. * Simple label-style cell node. Read its source for an example of custom <ASCellNode>s.
  166. */
  167. @interface ASTextCellNode : ASCellNode
  168. /**
  169. * Initializes a text cell with given text attributes and text insets
  170. */
  171. - (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets;
  172. /**
  173. * Text to display.
  174. */
  175. @property (nonatomic, copy) NSString *text;
  176. /**
  177. * A dictionary containing key-value pairs for text attributes. You can specify the font, text color, text shadow color, and text shadow offset using the keys listed in NSString UIKit Additions Reference.
  178. */
  179. @property (nonatomic, copy) NSDictionary *textAttributes;
  180. /**
  181. * The text inset or outset for each edge. The default value is 15.0 horizontal and 11.0 vertical padding.
  182. */
  183. @property (nonatomic, assign) UIEdgeInsets textInsets;
  184. /**
  185. * The text node used by this cell node.
  186. */
  187. @property (nonatomic, strong, readonly) ASTextNode *textNode;
  188. @end
  189. NS_ASSUME_NONNULL_END