九宫格

 CGFloat bookViewW = 100;

 CGFloat bookViewH = 140;

    //(屏幕的宽度 - 一个小块的宽度 * 总的列数) / (总的列数 + 1)

    int totalColum = 3;

    int totalRow = 4;    

    CGFloat marginX = (self.view.frame.size.width - bookViewW * totalColum) / (totalColum + 1);

    CGFloat marginY = (self.view.frame.size.height - bookViewH * totalRow) / (totalRow + 1);

    NSLog(@"mX %f,mY %f",marginX,marginY);

 for(int i = 0; i < 12; i++){        

     1.创建一个视图作为容器

        UIView * bookView = [[UIView alloc] init];

     2.建立父子关系,如果一个视图想要正确的显示在界面上,只要与一个已经显示在页面的视图建立父子关系就可以

        与控制器管理的默认视图,建立父子关系

        [self.view addSubview:bookView];

              //行         

           0,1,2 —>0

           3,4,5 ->1

           int row=i/totalColum

              //列

           0,3,6 ->0

           1,4,7 ->1

           2,5,8 ->2

           int col=i/totalRow

 

        //计算当前i 值所处在的列

        CGFloat currentColum = i % totalColum;// 0 - 2

        //计算当前i 值所处在的行

        CGFloat currentRow = i / totalColum;//0 - 3;

        CGFloat bookViewX = marginX + (marginX + bookViewW) * currentColum;

        CGFloat bookViewY = marginY + (marginY + bookViewH)  * currentRow;

      3.设置正确的Frame

        bookView.frame = CGRectMake(bookViewX, bookViewY, bookViewW, bookViewH);

        bookView.backgroundColor = [UIColor redColor];

 

 

posted on 2016-03-15 20:08  风过无痕CL  阅读(123)  评论(0编辑  收藏  举报

导航