Light's Blog

The best or nothing.

iOS知识小集-170417

| Comments

ComponentKit Tutorial - Component

CKComponent

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

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

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CKComponent *component = [CKComponent
    newWithView:{
        [UIView class],
        {
            {@selector(setBackgroundColor:),[UIColor redColor]},
            {@selector(setUserInteractionEnabled:), @YES},
            {CKComponentTapGestureAttribute(@selector(didTap))},
            {CKComponentViewAttribute::LayerAttribute(@selector(setCornerRadius:)), @10.0}
        }
    }
    size:{50,50}];

- (void)didTap{
  [self updateState:^(NSNumber *oldState){
    return [oldState boolValue] ? @NO : @YES;
  } mode:CKUpdateModeSynchronous];
}

CKLabelComponent

多行文字通过size.width控制。

1
2
3
4
5
6
7
8
9
10
CKLabelComponent *titleComponent = [CKLabelComponent newWithLabelAttributes:{
    .string = newsModel.title,
    .color = [UIColor whiteColor],
    .alignment = NSTextAlignmentLeft,
    .font = [UIFont systemFontOfSize:20]
  }
  viewAttributes:{
    { @selector(setBackgroundColor:), [UIColor clearColor] }
  }
  size:{}];

CKButtonComponent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 CKButtonComponent *buttonComponent = [CKButtonComponent
    newWithTitles:{
      {UIControlStateNormal,@"button"}
    }
    titleColors:{
      {UIControlStateNormal,[UIColor whiteColor]}
    }
    images:{}
    backgroundImages:{}
    titleFont:[UIFont systemFontOfSize:17]
    selected:NO
    enabled:YES
    action:{@selector(tapAction)}
    size:{}
    attributes:{}
    accessibilityConfiguration:{}];

CKImageComponent

1
2
3
4
5
6
7
CKImageComponent *image = [CKImageComponent newWithImage:
    [UIImage imageNamed:newsModel.imageURL]
    attributes:{
        { @selector(setBackgroundColor:), [UIColor whiteColor] },
        {CKComponentViewAttribute::LayerAttribute(@selector(setCornerRadius:)), @10.0}
    }
    size:{60, 60}];

CKInsetComponent

A component that wraps another component, applying insets around it.

1
2
3
4
5
+ (instancetype)newWithView:(const CKComponentViewConfiguration &)view
                     insets:(UIEdgeInsets)insets
                  component:(CKComponent *)component;

+ (instancetype)newWithInsets:(UIEdgeInsets)insets component:(CKComponent *)child;

Example:

1
2
3
4
5
6
CKInsetComponent *insetComponent = [CKInsetComponent
    newWithInsets:{.top = 10,.left = 20,.right = 10,.bottom = 20}
    component:[CKComponent
        newWithView:{}
        size:{}]
    ];

CKOverlayLayoutComponent

This component lays out a single component and then overlays a component on top of it streched to its size.

1
2
+ (instancetype)newWithComponent:(CKComponent *)component
                         overlay:(CKComponent *)overlay;

State

Simply ask three questions about each piece of data:
1. Is it passed in from a parent via props? If so, it probably isn’t state.
2. Does it remain unchanged over time? If so, it probably isn’t state.
3. Can you compute it based on any other state or props in your component? If so, it isn’t state.

For each piece of state in your application:
1. Identify every component that renders something based on that state.
2. Find a common owner component (a single component above all the components that need the state in the hierarchy).
3. Either the common owner or another component higher up in the hierarchy should own the state.
4. If you can’t find a component where it makes sense to own the state, create a new component simply for holding the state and add it somewhere in the hierarchy above the common owner component.

Scope

  1. Components that have state must have a scope.
  2. Components that have a controller must have a scope.
  3. Components that have child components with state or controllers may need a scope, even if they don’t have state or controllers.

ComponentController

与component写在同一个文件里,系统自动创建。
用来处理代理、事件响应等,有持续的生命周期。
scope必须唯一,与controller一一对应。

Comments