//允许scrollerView竖直移动
self.scrollView.alwaysBounceVertical = YES;
//允许scrollerView的可移动内容
self.scrollView.contentSize = CGSizeMake(SCREENWIDTH, 250);
//三列
int totalloc = 3;
CGFloat appvieww = 70;
CGFloat appviewh = 30;
//算出间距
CGFloat margin=(self.frame.size.width - totalloc * appvieww) / (totalloc + 1);
int count=11;
for (int i = 0; i < count; i++) {
int row = i / totalloc;//行号
//0/3=0,1/3=0,2/3=1; (i 从0到2都是第一行) i= 3的时候第二行
int loc = i % totalloc;//列号
//横坐标
CGFloat appviewx = margin + (margin + appvieww) * loc;
//纵坐标
CGFloat appviewy = margin + (margin + appviewh) * row;
//创建uiview控件
UIButton *appview=[[UIButton alloc]initWithFrame:CGRectMake(appviewx, appviewy, appvieww, appviewh)];
[appview setBackgroundColor:[UIColor purpleColor]];
[self.scrollView addSubview:appview];
}
总结一句计算出行号和列号就搞定大半了
就是这两行代码:
int row = i / totalloc;//行号
//0/3=0,1/3=0,2/3=1; (i 从0到2都是第一行) i= 3的时候第二行
int loc = i % totalloc;//列号