一 要求


完成下面的布局

二 分析


寻找规律,每一个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. struts2启动时,出现的com.opensymphony.xwork2.util.finder.ClassFinder - Unable to read class 错误解决办法

    在项目的struts.xml文件中第一行加入<constant name="struts.convention.package.locators" value="c ...

  2. SVN—使用总结

    SVN使用教程总结 为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版 ...

  3. url中是否加斜杠/

    通常来说,不加斜杠的形式(如”example.jsp”)请求的是相对于当前页面路径的资源 http://localhost:8080/webapp/examole:加斜杠的形式(”/example.j ...

  4. Yii2缓存依赖

  5. sitecore系列教程之目标功能有什么新意?

    由于SItecore 8中有很多令人兴奋的东西,我选择专注于体验平台的特定领域,这篇文章的主题是目标. 1.客户智能选项  目标项目(/ sitecore / system / Marketing C ...

  6. numpy高级索引

    布尔值索引 name_arr = np.array(["bob","joe","will","bob","jo ...

  7. 大数据和hadoop有什么关系?

    本文资料来自百度文库相关文档 Hadoop,Spark和Storm是目前最重要的三大分布式计算系统,Hadoop常用于离线的复杂的大数据处理,Spark常用于离线的快速的大数据处理,而Storm常用于 ...

  8. Linux服务器---邮件服务器dovecot

    安装dovecot Dovecot是CentOS系统中著名的POP3/IMAP服务器实现.POP3/IMAP是从邮件服务器中读取邮件时使用的协议,POP3将邮件全部从服务器中拖取下来:IMAP则每次从 ...

  9. 转:C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解

    转载自:http://www.cnblogs.com/easyfrog/p/3141269.html using System; using System.Collections.Generic; u ...

  10. AI赌神称霸德扑的秘密,刚刚被《科学》“曝光”了

    AI赌神称霸德扑的秘密,刚刚被<科学>“曝光”了 称霸德州扑克赛场的赌神Libratus,是今年最瞩目的AI明星之一. 刚刚,<科学>最新发布的预印版论文,详细解读了AI赌神背 ...