4.4 KiB
Executable File
title, layout, permalink, next
| title | layout | permalink | next |
|---|---|---|---|
| Getting Started | docs | /docs/getting-started.html | philosophy.html |
AsyncDisplayKit's basic unit is the node. ASDisplayNode is an abstraction
over UIView, which in turn is an abstraction over CALayer. Unlike views, which
can only be used on the main thread, nodes are thread-safe: you can
instantiate and configure entire hierarchies of them in parallel on background
threads.
To keep its user interface smooth and responsive, your app should render at 60 frames per second — the gold standard on iOS. This means the main thread has one-sixtieth of a second to push each frame. That's 16 milliseconds to execute all layout and drawing code! And because of system overhead, your code usually has less than ten milliseconds to run before it causes a frame drop.
AsyncDisplayKit lets you move image decoding, text sizing and rendering, and other expensive UI operations off the main thread. It has other tricks up its sleeve too... but we'll get to that later. :]
Nodes
If you're used to working with views, you already know how to use nodes. Most methods have a node equivalent and most UIView and CALayer properties are available as well. In any case where there is a naming discrepancy (such as .clipsToBounds vs .masksToBounds), nodes will default to the UIView name. The only exception is that nodes use position instead of center.
Of course, you can always access the underlying view or layer directly via node.view or node.layer.
Some of AsyncDisplayKit's core nodes include:
- ASDisplayNode. Counterpart to UIView — subclass to make custom nodes.
- ASCellNode. Counterpart to UICollectionViewCell or UITableViewCell — subclass to make custom cells or initialize with a view controller.
- ASControlNode. Analogous to UIControl — subclass to make buttons.
- ASImageNode. Like UIImageView — decodes images asynchronously.
- ASTextNode. Like UITextView — built on TextKit with full-featured rich text support.
Node Containers
When converting an app to use AsyncDisplayKit, a common mistake is to add nodes directly to an existing view hierarchy. Doing this will virtually guarantee that your nodes will flash as they are rendered.
Instead, you should add nodes as subnodes of one of the container classes. These classes are in charge of telling contained nodes what state they're currently in so that data can be loaded and nodes can be rendered as efficiently as possible. You should think of these classes as the integration point between UIKit and ASDK.
The four node containers are:
- ASViewController. A UIViewController subclass that allows you to provide the managed node.
- ASCollectionNode. Analogous to UICollectionView — manages a collection of ASCellNodes.
- ASTableNode. Analagous to UITableView — also uses ASCellNode but has with a fixed width.
- ASPagerNode. A specialized ASCollectionNode which can be used in the same way as a UIPageViewController.
Layout Engine
AsyncDisplayKit's layout engine is both one of its most powerful and one of its most unique features. Based on the CSS FlexBox model, it provides a declarative way of specifying a custom node's size and layout of its subnodes. While all nodes are concurrently rendered by default, asynchronous measurement and layout are performed by providing an ASLayoutSpec for each node.
The layout engine is based on the idea of ASLayouts which contain a position and size and ASLayoutSpecs which define various layouts conceptually and are used to output a calculated ASLayout. Layout specs can be composed of both child nodes as well as other layout specs which
A few layout specs:
- ASLayoutSpec. Produces sizes and positions for its related node.
- ASStackLayoutSpec. Allows you to lay out nodes in a very similar way to UIStackView.
- ASBackgroundLayoutSpec. Set a background for a node.
- ASStaticLayoutSpec. Useful when you want to manually define a static size in which to contain a set of nodes.