CKComponent Class Reference

Conforms to :
CKComponentStateType
__covariant
id
Declared in CKComponent.h
CKComponentSubclass.h

Overview

A component is an immutable object that specifies how to configure a view, loosely inspired by React.

Other Methods

+ newWithView:size:

A struct describing the view for this component. Pass {} to specify that no view should be created.

+ (instancetype)newWithView:(const CKComponentViewConfiguration &)view size:(const CKComponentSize &)size

Parameters

view

A struct describing the view for this component. Pass {} to specify that no view should be created.

size

A size constraint that should apply to this component. Pass {} to specify no size constraint.

@example A component that renders a red square: [CKComponent newWithView:{[UIView class], {{@selector(setBackgroundColor:), [UIColor redColor]}}} size:{100, 100}]

Declared In

CKComponent.h

– viewContext

While the component is mounted, returns information about the component’s manifestation in the view hierarchy.

- (CKComponentViewContext)viewContext

Discussion

If this component creates a view, this method returns the view it created (or recycled) and a frame with origin 0,0 and size equal to the view’s bounds, since the component’s size is the view’s size.

If this component does not create a view, returns the view this component is mounted within and the logical frame of the component’s content. In this case, you should not make any assumptions about what class the view is.

Declared In

CKComponent.h

– nextResponder

While the component is mounted, returns its next responder. This is the first of: - Its component controller, if it has one; - Its supercomponent; - The view the component is mounted within, if it is the root component.

- (id)nextResponder

Declared In

CKComponent.h

Other Methods

– 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 children’s 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

+ initialState

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

+ (CKComponentStateType)initialState

Declared In

CKComponentSubclass.h

– state

Returns the component’s state if available. Can be called only after the component’s creation is done.

- (CKComponentStateType)state

Declared In

CKComponentSubclass.h

– updateState:mode:

Enqueue a change to the state.

- (void)updateState:(CKComponentStateType ( ^ ) ( CKComponentStateType currentState ))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

– animationsOnFinalUnmount

Experimental API, do not use

- (std : : vector<CKComponentFinalUnmountAnimation>)animationsOnFinalUnmount

Discussion

Override to return the list of component / animation pairs that will run when this component is no longer in the mounted hierarchy. You can return multiple animations for this component or specify animations for any child components.

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