九宫格布局方法抽取

  • 测试效果


    九宫格布局方法抽取_第1张图片
    Sudoku.png
  • 使用时只需要一行代码

[Sudoku sudokuWithColumn:5 line:4 objectWidth:65 height:75 inTheSuperView:_testView];
  • 单独写了个类
#import "Sudoku.h"

@implementation Sudoku

/**
 * 核心思路:
 * 列的x值一样,行的y值一样
 * x值=列号 *(子控件宽+列间距),y值=行号 *(子控件高+行间距)
 * 列号=index%列数,行号=index/列数    ----计算时依靠固定列数
 */
+ (void)sudokuWithColumn:(NSUInteger)column line:(NSUInteger)line objectWidth:(CGFloat)width height:(CGFloat)height inTheSuperView:(UIView *)theSuperView{
    
    // 预定义
    CGFloat objectW = width;
    CGFloat objectH = height;
    NSUInteger cols = column;
    NSUInteger lines = line;
    
    // margin计算
    CGFloat colMargin = (theSuperView.frame.size.width - cols * objectW)/(cols - 1);
    CGFloat rowMargin = (theSuperView.frame.size.height - lines * objectH)/(lines - 1);
    
    // 父控件中已经添加的子控件个数
    NSUInteger index = theSuperView.subviews.count;
    
    // x、y值计算(位置)
    NSUInteger col = index % cols;
    NSUInteger row = index / cols;
    
    CGFloat objectX = col * (objectW + colMargin);
    CGFloat objectY = row * (objectH + rowMargin);
    
    // 创建要添加的控件
    UIView *objectView = [[UIView alloc] init];
    objectView.backgroundColor = [UIColor yellowColor];
    objectView.frame = CGRectMake(objectX, objectY, objectW, objectH);
    [theSuperView addSubview:objectView];
    
    theSuperView.clipsToBounds = YES;
}
@end

你可能感兴趣的:(九宫格布局方法抽取)