一 要求


完成下面的布局

二 分析


寻找规律,每一个UIView的x坐标和y坐标

三 实现思路


(1) 明确每一块用得是什么View;

(2) 明确每个View之间的父子关系,每个视图都只有一个父视图,拥有很多的子视图;

(3) 可以先尝试逐个的添加格子,最后考虑使用for循环,完成所有的UIView创建;

(4) 加载app数据,根据数据长度创建对应个数的格子;

(5) 添加格子内部的子控件

(6) 给内部的子控件装配数据

四 代码示例


 //
// RootViewController.m
// 九宫格坐标计算
//
// Created by lovestarfish on 15/11/1.
// Copyright © 2015年 S&G. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()
//* 数据源数组 /
@property (nonatomic,retain) NSArray *apps; @end @implementation RootViewController - (NSArray *)apps {
//1. 加载数据
if (!_apps) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.apps = dic[@"app"];
}
return _apps;
} - (void)viewDidLoad {
[super viewDidLoad]; self.title = @"九宫格展示"; //2. 完成布局设计
//三列
int totalloc = ;
CGFloat appViewWidth = ;
CGFloat appViewHeight = ;
CGFloat margin = (self.view.frame.size.width- totalloc * appViewWidth) / (totalloc + );
int appCount = (int)self.apps.count;
for (int i = ; i < appCount; i++) {
int row = i / totalloc;//行号
int loc = i % totalloc;//列号
CGFloat appViewX = margin + (margin + appViewWidth) * loc;
CGFloat appViewY = margin + (margin + appViewHeight) * row;
//创建UIView控件
UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(appViewX, appViewY + , appViewWidth, appViewHeight)];
[self.view addSubview:appView];
[appView release]; //创建UIView控件中的子视图
UIImageView *appImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
appImageView.image = [UIImage imageNamed:self.apps[i][@"image"]];
appImageView.contentMode = UIViewContentModeScaleAspectFit;
[appView addSubview:appImageView];
[appImageView release]; //创建文本标签
UILabel *appLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
appLabel.text = self.apps[i][@"name"];
appLabel.textAlignment = NSTextAlignmentCenter;
appLabel.font = [UIFont systemFontOfSize:];
[appView addSubview:appLabel];
[appLabel release]; //创建按钮
UIButton *appButton = [UIButton buttonWithType:UIButtonTypeSystem];
appButton.frame = CGRectMake(, , , );
[appButton setTitle:@"下载" forState:UIControlStateNormal];
appButton.backgroundColor = [UIColor greenColor];
appButton.titleLabel.font = [UIFont systemFontOfSize:];
[appButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[appView addSubview:appButton];
[appButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
} /**
* 下载按钮的响应事件
*/
- (void)click {
//动画标签
UILabel *animaLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x - , self.view.center.y - + , , )];
animaLabel.text = @"下载成功";
animaLabel.font = [UIFont systemFontOfSize:];
animaLabel.textAlignment = NSTextAlignmentCenter;
animaLabel.backgroundColor = [UIColor brownColor];
animaLabel.alpha = ;
[self.view addSubview:animaLabel];
[animaLabel release]; //执行完之后,还得把这给删除了,推荐使用block动画
[UIView animateWithDuration:4.0 animations:^{
animaLabel.alpha = 1.0;
} completion:^(BOOL finished) {
[animaLabel removeFromSuperview];
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end

五 执行效果


 

iOS开发 -------- 九宫格坐标计算的更多相关文章

  1. iOS开发UI篇—九宫格坐标计算

    iOS开发UI篇—九宫格坐标计算 一.要求 完成下面的布局 二.分析 寻找左边的规律,每一个uiview的x坐标和y坐标. 三.实现思路 (1)明确每一块用得是什么view (2)明确每个view之间 ...

  2. iOS开发UI篇——九宫格坐标计算

    一.要求 完成下面的布局 二.分析 寻找左边的规律,每一个uiview的x坐标和y坐标. 三.实现思路 (1)明确每一块用得是什么view (2)明确每个view之间的父子关系,每个视图都只有一个父视 ...

  3. iOS开发 百度坐标点的计算

    BMKMapPoint point1 = BMKMapPointForCoordinate(_userGps); BMKMapPoint point2 = BMKMapPointForCoordina ...

  4. iOS开发小技巧--计算label的Size的方法总结

    计算label的Size方法 sizeWithAttributes:方法 适用于不换行的情况,宽度不受限制的情况 /// 根据指定文本和字体计算尺寸 - (CGSize)sizeWithText:(N ...

  5. iOS开发 火星坐标转百度坐标

    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(latitude, longitude);//原始坐标 //转换 google地图.s ...

  6. iOS开发 百度坐标转火星坐标

    - (CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps { const double x_pi = 3.141 ...

  7. iOS开发基础-九宫格坐标(1)

    一.功能分析 1)以九宫格展示图片信息,每一个 UIView 包含一个 UIImageView .一个 UILabel 和一个 UIButton . 2)加载App数据,根据数据长度创建对应的格子数: ...

  8. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

  9. iOS开发基础-九宫格坐标(6)

    继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...

随机推荐

  1. Sublime text3 经常出现 “ There are no packages avaliable for installation” 解决方法

    对应这个问题,一开始在度娘上找到很多答案,包括将json文件放在本地然后通过 package setting 更改的,发现其实不好使,原因未知. 后来测试了在hosts文件添加sublime text ...

  2. 初识Vue,简单的todolist

    vue开发源码:https://vuejs.org/js/vue.js todolist代码: <!DOCTYPE html> <html lang="en"&g ...

  3. Oracle查询表占用空间的大小

    select * from (select OWNER, segment_name, segment_type, sum(bytes) mmm from dba_segments where /*ta ...

  4. Oracle表空间管理相关

    以下以我自己的测试环境举例: 1.表空间的 block_size 为 8192字节,即8KBytes.从数据字典中查到 max_size 为 2147483645,即约为15.9TBytes. 2.在 ...

  5. 关于CTeX的几个大坑

    https://blog.csdn.net/zjutczj/article/details/53463478 最近一直忙着写小论文,毕业设计中期答辩,没有更新博客,忙过这一阵我会把这段时间学习机器学习 ...

  6. Gardener Bo (树剖 + 问题分解)

    一开始没看懂计算答案的第四部和update2,很迷.然后一直推敲之后才发下我计算的时候漏掉一个关键点.没有把加值的影响放到父节点上. #include<bits/stdc++.h> #de ...

  7. 20155228 2016-2017-2 《Java程序设计》第6周学习总结

    20155228 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 输入与输出 在Java中输入串流代表对象为java.io.InputStream实例,输出串 ...

  8. 【安装虚拟机一】配置VMware

    安装软件 VMware 10 CentOS-6.5-x86_64-minimal.iso 第一步:打开VMware 10  主页选择 “创建新的虚拟机” 第二步:选择自定义设置 第三步:设置虚拟机兼容 ...

  9. 为什么C++函数形参默认值从最末一个赋值?

    [1]函数调用时形参的压栈顺序 1.示例代码如下(VS2010): #include <iostream> using namespace std; ); void fun(int a, ...

  10. [转载]asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别

    在asp.net中经常出现包含这种形式<%%>的html代码,总的来说包含下面这样几种格式: 一. <%%> 这种格式实际上就是和asp的用法一样的,只是asp中里面是vbsc ...