一、功能分析

  1)以九宫格展示图片信息,每一个 UIView 包含一个 UIImageView 、一个 UILabel 和一个 UIButton 。

  2)加载App数据,根据数据长度创建对应的格子数;

  3)点击下载按钮后,做出相应操作。

二、九宫格信息分析

三、实例代码

  新建Plist属性文件,并命名为Data,修改属性列表成如下形式:

  

  定义每一个 UIview 的宽度和高度:

//ViewController.m
const CGFloat appViewWidth = ; //子视图宽度
const CGFloat appViewHeight = ; //子视图高度

  在类扩展中声明 NSArray 属性,用于存放属性列表中的数据:

@interface ViewController ()
@property (nonatomic, weak) NSArray *apps;
@end

  懒加载 apps 属性,即重写 getter 方法:

 - (NSArray *)apps {
if (!_apps) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
_apps = [NSArray arrayWithContentsOfFile:path];
}
return _apps;
}

  修改 viewDidLoad 方法,让其加载视图:

 - (void)viewDidLoad {
[super viewDidLoad]; int totalColumn = ; //3列
CGFloat margin = (self.view.frame.size.width - totalColumn*appViewWidth) / (totalColumn + );
int count = self.apps.count;
NSLog(@"%d", count); for (int i = ; i < count; i++) {
int row = i/totalColumn; //行号,从0开始
int column = i%totalColumn; //列号,从0开始
CGFloat appViewX = margin + (margin + appViewWidth) * column; //子视图的X坐标
CGFloat appViewY = margin + (margin + appViewHeight) * row; //子视图的Y坐标 //创建UIView控件
UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(appViewX, appViewY, appViewWidth, appViewHeight)];
[self.view addSubview:appView];
//创建上述UIView控件的子视图,创建图像信息
UIImageView *appImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
UIImage *appImage = [UIImage imageNamed:self.apps[i][@"name"]];
appImageView.image = appImage;
[appImageView setContentMode:UIViewContentModeScaleAspectFit];
[appView addSubview:appImageView];
//创建上述UIView控件的子视图,创建UILabel信息描述标签
UILabel *appLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[appLabel setText:self.apps[i][@"desc"]];
[appLabel setTextAlignment:NSTextAlignmentCenter];
[appLabel setFont:[UIFont systemFontOfSize:12.0]];
[appLabel setTextColor:[UIColor blueColor]];
[appView addSubview:appLabel];
//创建下载按钮
UIButton *appButton = [UIButton buttonWithType:UIButtonTypeCustom];
appButton.frame = CGRectMake(, , , );
[appButton setBackgroundImage:[UIImage imageNamed:@"download"] forState:UIControlStateNormal];
[appButton setBackgroundImage:[UIImage imageNamed:@"downloaded"] forState:UIControlStateHighlighted];
[appButton setTitle:@"下载" forState:UIControlStateNormal];
appButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
[appButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[appView addSubview:appButton];
[appButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
}

  创建下载按钮的响应事件:

 - (void)buttonClicked:(UIButton *)button {
//动画标签
UILabel *animaLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-, self.view.center.y+, , )];
[animaLabel setText:@"下载成功"];
[animaLabel setTextAlignment:NSTextAlignmentCenter];
animaLabel.font = [UIFont systemFontOfSize:12.0];
[animaLabel setBackgroundColor:[UIColor brownColor]];
[animaLabel setAlpha:0.0];
[self.view addSubview:animaLabel]; [UIView animateWithDuration:4.0 animations:^{
//逐渐显示标签
[animaLabel setAlpha:1.0];
} completion:^(BOOL finished) {
//动画结束时,移除显示下载完成的标签
[animaLabel removeFromSuperview];
//已下载时,改变按钮标题,及背景图片
[button setTitle:@"已下载" forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"downloaded"] forState:UIControlStateNormal];
//已下载完成的,取消按钮下载触发事件
[button removeTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}];
}

实例:

参考博客:iOS开发UI篇—九宫格坐标计算

实例代码:http://pan.baidu.com/s/1qXyZhWK

iOS开发基础-九宫格坐标(1)的更多相关文章

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

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

  2. iOS开发基础-九宫格坐标(5)

    继续在iOS开发基础-九宫格坐标(4)的基础上进行优化. 一.改进思路 1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21.22行对控件属性的设置能否拿到视图类 WJQAp ...

  3. iOS开发基础-九宫格坐标(4)

    对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...

  4. iOS开发基础-九宫格坐标(3)之Xib

    延续iOS开发基础-九宫格坐标(2)的内容,对其进行部分修改. 本部分采用 Xib 文件来创建用于显示图片的 UIView 对象. 一.简单介绍  Xib 和 storyboard 的比较: 1) X ...

  5. iOS开发基础-九宫格坐标(2)之模型

    在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...

  6. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  7. iOS开发——总结篇&IOS开发基础知识

    IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...

  8. IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  9. iOS开发基础-图片切换(4)之懒加载

    延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善. 一.懒加载基本内容 懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法. 注意:懒加载时一定要先判断该 ...

随机推荐

  1. Jenkins结合.net平台之ftp客户端

    上一节我们讲解了如何配置ftp服务端,本节我们讲解如何使用winscp搭建ftp客户端,为什么使用winscp而不是filezilla客户端版,前面我们简单说过,这里不再赘述. 下载winscp以后我 ...

  2. javascript sort 函数用法

    sort 函数 博客地址:https://ainyi.com/41 简单的说,sort() 在没有参数时,返回的结果是按升序来排列的.即字符串的Unicode码位点(code point)排序 [5, ...

  3. 第50章 设备授权端点(Device Authorization Endpoint) - Identity Server 4 中文文档(v1.0.0)

    设备授权端点可用于请求设备和用户代码.此端点用于启动设备流授权过程. 注意 终端会话端点的URL可通过发现端点获得. client_id 客户标识符(必填) client_secret 客户端密钥可以 ...

  4. docker 创建mysql镜像,并成功进行远程连接

    1.安装 1.1 拉取镜像 docker pull mysql 拉取成功可以验证一下 docker images 1.2 创建并启动一个mysql容器 docker run --name ly-mys ...

  5. Asp.Net Core中使用MongoDB的入门教程,控制台程序使用 MongoDB

    内容来源  https://blog.csdn.net/only_yu_yy/article/details/78882446 首先,创建一个.Net Core的控制台应用程序.然后使用NuGet导入 ...

  6. dogse入门指南

    dogse入门指南 Dogse作为游戏服务端引擎,目前只包含游戏服务端的核心部分,但这也是最核心的部分.它全部使用.net c#开发,充分兼顾了程序性能与代码编写的准确性与易用性,再加上以vs作为开发 ...

  7. 微信小程序解密得到unoinid和手机号 (开放数据的校验和解密 获取手机号)

    实际测试 两种方法都可以: 第一种方法: public static string DecodeUserInfo(string encryptedData, string iv, string cod ...

  8. AJAX跨站处理解决方案

    //直接使用ajax会提示跨站失败 $.ajax({ type : 'POST', url : 'http://www.abc.com/api', data : '', dataType : 'tex ...

  9. Python:运算类内建函数列举

    1. divmod() python3.x版本中,整除运算用 “//”,取余可以用 “%”,在某些问题中要同时得到商和余数就需要两步运算,而使用divmod函数可以同时得到商和余数: 函数有两个参数d ...

  10. dede 采集到数据后,发布日期变为本地日期解决方法

    找到dede目录下的co_export.php 大概在170行左右 //获取时间和标题 $pubdate = $sortrank = time(); $title = $row->title; ...