BBView 是一个 UIView 子类,利用基于块编程的力量,允许对 UIView 进行更大的非子类化自定义。BBView 有三个主要组件:布局块、手势识别器动作块和一个代理协议,后者在“边缘”上“清理了一下”,如果需要的自定义不仅仅是布局块和动作块,则允许进行更大的自定义。第一个组件,布局块,实现了 BBView 的 setFrameBlock 和 layoutSubviewsBlock 属性。为了补充这一点,可以为子视图设置字符串标识符,以便在布局块中访问它们。这些各自允许开发者向标准的 UIView 方法 setFrame 和 layoutSubviews 中添加代码。下面给出一个代码示例。
-(void)function{
BBView *super=[[BBView alloc] init];
UIView *sub=[[UIView alloc] init];
[super addSubview:sub]
[super setIdentifier:@"subview" forSubview:sub];
//Note that setting an identifier for a subview will add it as a subview if it is not one already. The addSubview line could be omitted.
super.frameChangeBlock=^(BBView *view){
[(BBView*)view subViewForIdentifier:@"subview"].frame=view.bounds;
};
super.frame=CGRectMake(0,0,100,100);
//sub's frame is now also CGRectMake(0,0,100,100)
}