1. Layout,Element和Model
在进一步了解TangramView之间,需要了解Layout,Element和Model。这3部分在实现中通常是由protocol定义需要的属性或者接口,通过继承来扩展一些属性,然后通过相应的工厂类(接口)来创建。
1.1 布局TangramLayoutProtocol及其工厂TangramLayoutFactoryProtocol
TangramLayoutProtocol
JSON中的layout对应于哪一个LayoutClass是根据“type”字段决定的,工厂类有一个“type”->“layoutClass”的映射,通过这个映射来获取class。
属性:
NSString *identifier; // 与JSON中的“id”字段对应,是构成muiID的一部分
NSArray *itemModels; // itemModel
@property (nonatomic, weak) TangramBus *tangramBus;
方法:
-(TangramLayoutType *)layoutType; // TangramLayoutType是一个字符串类型,layoutType构成muiID的一部分
-(void)calculateLayout; // Calculate itemModel position, 而且还计算出了layout自己的frame,这个方法会在tangramView的reloadData->layoutContentWithCalculateLayout方法中调用。
-(void)addSubView:(UIView *)view withModel:(NSObject<TangramItemModelProtocol> *)model;
TangramFlowLayout : UIView<TangramLayoutProtocol>
属性:
NSObject<TangramItemModelProtocol> *headerItemModel; // 与JSON中的”header”字段对应,解析为一个itemModel
NSObject<TangramItemModelProtocol> *footerItemModel; // 与JSON中的”footer”字段对应,解析为一个itemModel
1.2 模型TangramModelProtocol及其工厂TangramItemModelFactoryProtocol
TangramItemModelProtocol
属性:
CGRect itemFrame; // itemModel的frame,layout通过itemModel的各种属性计算出itemModel的frame之后,会设置itemModel的frame。
@optional
CGRect absRect; // 相对于LazyScrollView的frame信息
NSString *muiID; // item Model对应的element在LazyScrollView中的唯一标识符
方法:
-(TangramItemType *)itemType; // TangramItemType是一个字符串类型,TangramItemType构成muiID的一部分,通常返回的是self.type属性。
-(NSString *)reuseIdentifier; // Reuseidentifier for LazyscrollView, 也是构成了muiID的一部分(查看:ItemModel的reuseIdentifier以及复用)。
TangramDefaultItemModel:TMLazyItemModel<TangramItemModelProtocol>
属性:
NSString *type; // 与JSON中item的”type”字段对应
NSString *specificReuseIdentifier; // 与JSON中的item的“reuseId”字段有关, Specific ReuseIdentifier,
BOOL disableReuse; // 与JSON中的item的”disableReuse”字段有关,Whether disable reuse,
NSMutableDictionary *bizDict // JSON中的item中除了”type”和“style”字段,其他字段都存放在bizDict中。
NSMutableDictionary *styleDict; // JSON中item的”style”的中的除了”margin,display,colspan,height,width”之外的其他字段,
NSString *layoutIdentifierForLayoutModel; // Layout identifier, 对应的是JSON中item的“id”字段,
NSString *linkElementName; // Binded element(UIView or its subclass) classname, 对应的是type字段value为key,在工厂elementTypeMap字典中的value值。
NSUInteger index; // Index in layout,
NSArray *margin; // 与JSON中item的”style”的”margin”字段有关
CGFloat heightFromStyle; // 与JSON中item的”style”的“height”字段有关
CGFloat widthFromStyle; // 与JSON中item的“style”的“width”字段有关
NSString *position; // 与JSON中item的”style”的”position”字段有关
TMLazyItemModel
属性:
NSString *muiID; // item view在LazyScrollView的唯一标识符。
CGRect absRect; // item view在LazyScrollView中的frame信息,是相对于LazyScrollView的相对信息
1.3 视图element(UIView)及其工厂TangramElementFactoryProtocol
element的class是用过itemModel的linkElementName决定的,
element的属性是自定义的,是从itemModel的bizDict和styleDict的key-value赋值给element中以key为属性名的property中。
TangramElementHeightProtocol
+(CGFloat)heightByModel:(TangramDefaultItemModel *)itemModel;
TMLazyItemViewProtocol
AOP方法
UIView+TMLazyScrollView
属性:
NSString *reuseIdentifier; // 通过itemModel的reuseIdentifier进行赋值
TangramEasyElementProtocol
方法:
-(TangramDefaultItemModel *)tangramItemModel; // 获取与element相关的itemModel
-(void)setTangramItemModel: (TangramDefaultItemModel *)tangramItemModel; // 设置与element相关的itemModel
2. TangramView
属性:
一些layout的字典,等等。
方法:
一. 对外部提供了- (void)reloadData方法
1.1 清理自己的属性,然后根据VC实现的代理填充这些属性。
1.2 -(void)layoutContentWithCalculateLayout:(BOOL)calculate
计算所有layout中itemModel的frame,并进行设置,而且也计算出了layout的frame。
并设置自己的contentSize。将自己的contentOffset设置为原来的contentOffset,也就是reloadData并不会改变contentOffset。
1.3 调用父类,也就是TMLazyScrollView的reloadData方法。
二. 实现了MTLazyScrollView需要的代理方法
-
-(NSUInteger)numberOfItemsInScrollView:(TMLazyScrollView *)scrollView
返回全部的layout中共有多少个itemModel;并将itemModel的index flat之后保存一些本地的字典。 -
-(TMLazyItemModel *)scrollView:(TMLazyScrollView *)scrollView itemModelAtIndex:(NSUInteger)index
设置第index个itemModel的lazyModel的muiID属性,absRect属性,如果itemModel也有这两个属性,那么也复制给它们。 -
-(UIView *)scrollView:(TMLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID
根据lazyScrollView提供的muiID,返回一个element,这个element是通过vc的代理返回的(复用或者新创建),并将element加入到layout视图中,并设置element的frame。
网友评论