CKComponent() Category Reference

Conforms to CKScopedComponent
Declared in CKComponentInternal.h
CKComponentSubclass.h

Overview

A constant that indicates that the parent’s size is not yet determined in either dimension.

Other Methods

– mountInContext:size:children:supercomponent:

Mounts the component in the given context: - Stores references to the supercomponent and superview for -nextResponder and -viewConfiguration. - Creates or updates a controller for this component, if one should exist. - If this component has a view, creates or recycles a view by fetching one from the given MountContext, and: - Unmounts the view’s previous component (if any). - Applies attributes to the view. - Stores a reference to the view in _mountedView (for -viewContext, transient view state, and -unmount). - Stores a reference back to this component in view.ck_component. (This sets up a retain cycle which must be torn down in -unmount.)

- (CK : : Component : : MountResult)mountInContext:(const CK : : Component : : MountContext &)context size:(const CGSize)size children:(std : : shared_ptr<conststd::vector<CKComponentLayoutChild> >)children supercomponent:(CKComponent *)supercomponent

Parameters

context

The component’s content should be positioned within the given view at the given position.

size

The size for this component

children

The positioned children for this component. Normally this parameter is ignored.

supercomponent

This component’s parent component

Return Value

An updated mount context. In most cases, this is just be the passed-in context. If a view was created, this is used to specify that subcomponents should be mounted inside the view.

Discussion

Override this if your component wants to perform a custom mounting action, but this should be very rare!

Declared In

CKComponentInternal.h

– unmount

Unmounts the component: - Clears the references to supercomponent and superview. - If the component has a mountedView: - Clears the view’s reference back to this component in ck_component. - Clears mountedView.

- (void)unmount

Declared In

CKComponentInternal.h

– childrenDidMount

Called when the component and all its children have been mounted.

- (void)childrenDidMount

Declared In

CKComponentInternal.h

  rootComponentMountedView

Used to get the root component in the responder chain; don’t touch this.

@property (nonatomic, weak) UIView *rootComponentMountedView

Declared In

CKComponentInternal.h

  scopeFrameToken

For internal use only; don’t touch this.

@property (nonatomic, strong, readonly) id<NSObject> scopeFrameToken

Declared In

CKComponentInternal.h

  size

The size that was passed into the component; don’t touch this.

@property (nonatomic, assign, readonly) CKComponentSize size

Declared In

CKComponentInternal.h

Other Methods

+ initialState

Called to get the component’s initial state; the default implementation returns nil.

+ (id)initialState

Declared In

CKComponentSubclass.h

– layoutThatFits:parentSize:

Call this on children components to compute their layouts within your implementation of -computeLayoutThatFits:.

- (CKComponentLayout)layoutThatFits:(CKSizeRange)constrainedSize parentSize:(CGSize)parentSize

Parameters

constrainedSize

Specifies a minimum and maximum size. The receiver must choose a size that is in this range.

parentSize

The parent component’s size. If the parent component does not have a final size in a given dimension, then it should be passed as kCKComponentParentDimensionUndefined (for example, if the parent’s width depends on the child’s size).

Return Value

A struct defining the layout of the receiver and its children.

Discussion

Warning: You may not override this method. Override -computeLayoutThatFits: instead.

Warning: In almost all cases, prefer the use of CKComputeComponentLayout in CKComponentLayout

Declared In

CKComponentSubclass.h

– computeLayoutThatFits:

Override this method to compute your component’s layout.

- (CKComponentLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize

Parameters

constrainedSize

A min and max size. This is computed as described in the description. The CKComponentLayout you return MUST have a size between these two sizes. This is enforced by assertion.

Discussion

Why do you need to override -computeLayoutThatFits: instead of -layoutThatFits:parentSize:? The base implementation of -layoutThatFits:parentSize: does the following for you: 1. First, it uses the parentSize parameter to resolve the component’s size (the one passed into -initWithView:size:). 2. Then, it intersects the resolved size with the constrainedSize parameter. If the two don’t intersect, constrainedSize wins. This allows a component to always override its childrens' sizes when computing its layout. (The analogy for UIView: you might return a certain size from -sizeThatFits:, but a parent view can always override that size and set your frame to any size.)

Declared In

CKComponentSubclass.h

– computeLayoutThatFits:restrictedToSize:relativeToParentSize:

CKComponent’s implementation of -layoutThatFits:parentSize: calls this method to resolve the component’s size against parentSize, intersect it with constrainedSize, and call -computeLayoutThatFits: with the result.

- (CKComponentLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize restrictedToSize:(const CKComponentSize &)size relativeToParentSize:(CGSize)parentSize

Discussion

In certain advanced cases, you may want to customize this logic. Overriding this method allows you to receive all three parameters and do the computation yourself.

Warning: Overriding this method should be done VERY rarely.

Declared In

CKComponentSubclass.h

– updateState:mode:

Enqueue a change to the state.

- (void)updateState:(id ( ^ ) ( id ))updateBlock mode:(CKUpdateMode)mode

Parameters

updateBlock

A block that takes the current state as a parameter and returns an instance of the new state.

mode

The update mode used to apply the state update. @@see CKUpdateMode

Discussion

The state must be immutable since components themselves are. A possible use might be:

[self updateState:^MyState (MyState currentState) { MyMutableState *nextState = [currentState mutableCopy]; [nextState setFoo:[nextState bar] * 2]; return [nextState copy]; // immutable! :D }];

Declared In

CKComponentSubclass.h

– targetForAction:withSender:

Allows an action to be forwarded to another target. By default, returns the receiver if it implements action, and proceeds up the responder chain otherwise.

- (id)targetForAction:(SEL)action withSender:(id)sender

Declared In

CKComponentSubclass.h

– canPerformAction:withSender:

When an action is triggered, a component may use this method to either capture or ignore the given action. The default implementation simply uses respondsToSelector: to determine if the component can perform the given action.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

Discussion

In practice, this is useful only for integrations with UIMenuController whose API walks the UIResponder chain to determine which menu items to display. You should not override this method for standard component actions.

Declared In

CKComponentSubclass.h

– animationsOnInitialMount

Override to return a list of animations that will be applied to the component when it is first mounted.

- (std : : vector<CKComponentAnimation>)animationsOnInitialMount

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– animationsFromPreviousComponent:

Override to return a list of animations from the previous version of the same component.

- (std : : vector<CKComponentAnimation>)animationsFromPreviousComponent:(CKComponent *)previousComponent

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– boundsAnimationFromPreviousComponent:

Override to return how the change to the bounds of the root component should be animated when updating the hierarchy.

- (CKComponentBoundsAnimation)boundsAnimationFromPreviousComponent:(CKComponent *)previousComponent

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– viewForAnimation

Attempts to return a view suitable for rendering an animation.

- (UIView *)viewForAnimation

Discussion

Since a component may or may not be backed by a view nil may be returned. Composite components may, given the fact they are composed of other components, return the animatable view of its descendant. As a rule of thumb:

  1. . CKComponent subclasses backed by a view will return the backing view
  2. . CKComponent subclasses not backed by a view will return nil
  3. . CKCompositeComponent subclasses backed by a view will return the backing view
  4. . CKCompositeComponent subclasses not backed by a view will return the animatable view of its descendant

This method may be overridden in rare situations where a more suitable view should be used for rendering animations.

Declared In

CKComponentSubclass.h

– controller

Returns the component’s controller, if any.

- (CKComponentController *)controller

Declared In

CKComponentSubclass.h