九宫格的实现
Posted Hunter_Wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了九宫格的实现相关的知识,希望对你有一定的参考价值。
// // ViewController.m // 九宫格的实现 // // Created by Hunter_Wang on 15/11/27. // Copyright © 2015年 HaiTeng. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /////////////////////////////////////// // 搭建界面,九宫格 #define kAppViewW 70 #define kAppViewH 30 #define kColCount 4 #define kStartY 20 // 320 - 3 * 80 = 80 / 4 = 20 CGFloat marginX = (self.view.bounds.size.width - kColCount * kAppViewW) / (kColCount + 1); CGFloat marginY = 10; for (int i = 0; i < 7; i++) { // 行 // 0, 1, 2 => 0 // 3, 4, 5 => 1 int row = i / kColCount; // 列 // 0, 3, 6 => 0 // 1, 4, 7 => 1 // 2, 5, 8 => 2 int col = i % kColCount; CGFloat x = marginX + col * (marginX + kAppViewW); CGFloat y = kStartY + marginY + row * (marginY + kAppViewH); UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)]; appView.backgroundColor = [UIColor redColor]; appView.layer.masksToBounds = YES; appView.layer.cornerRadius = 15; appView.layer.borderWidth = 1; appView.layer.borderColor = [UIColor grayColor].CGColor; [self.view addSubview:appView]; } /////////////////////////////////////// } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上是关于九宫格的实现的主要内容,如果未能解决你的问题,请参考以下文章
Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现