iOS UI基础-4.0应用程序管理
功能与界面
功能分析:
- 以九宫格的形式展示应用信息
- 点击下载按钮后,做出相应的操作
- 加载应用信息
- 根据应用的个数创建对应的view
- 监听下载按钮点击
整个应用界面:

程序实现
思路
- UI布局:N宫格
- 事件监听
- 动态添加 (by plist)
- 整体封装,组合每个应用信息,使用View的层级包装帮助布局
项目工程

纯代码
//
// UYViewController.m
// 4.0应用程序管理
//
// Created by jiangys on 15/8/23.
// Copyright (c) 2015年 uxiaoyuan. All rights reserved.
// #import "UYViewController.h" #define kAppViewW 80
#define kAppViewH 90
#define kColCount 3
#define kStartY 20 @interface UYViewController () /** 存放应用信息*/
@property(nonatomic,strong) NSArray *appList;
@end @implementation UYViewController -(NSArray *) appList
{
if (nil==_appList) {
NSString *path=[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
_appList=[NSArray arrayWithContentsOfFile:path];
}
return _appList;
} - (void)viewDidLoad
{
[super viewDidLoad]; //计算边距
CGFloat marginX=(self.view.bounds.size.width-kColCount * kAppViewW)/(kColCount + );
CGFloat marginY=; for (int i=; i<self.appList.count; i++) { NSDictionary *appData=self.appList[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)];
[self.view addSubview:appView]; // 1> UIImageView
UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(, , kAppViewW, )];
// 设置图像
icon.image = [UIImage imageNamed:appData[@"icon"]];
// 设置图像填充模式,等比例显示(CTRL+6)
icon.contentMode = UIViewContentModeScaleAspectFit;
[appView addSubview:icon]; // 2> UILabel -> 应用程序名称
// CGRectGetMaxY(frame) = frame.origin.y + frame.size.height
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(icon.frame), kAppViewW, )];
lable.text = appData[@"name"]; // 设置字体
lable.font = [UIFont systemFontOfSize:13.0];
lable.textAlignment = NSTextAlignmentCenter; [appView addSubview:lable]; // 3> UIButton -> 下载按钮
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(lable.frame), kAppViewW, )];
button.backgroundColor = [UIColor yellowColor]; // 背景图片
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
// 按钮都是有状态的,不同状态可以对应不同的标题
[button setTitle:@"下载" forState:UIControlStateNormal];
// 修改字体(titleLabel是只读的)
// readonly表示不允许修改titleLabel的指针,但是可以修改label的字体
// 提示:按钮的字体是不区分状态的!
button.titleLabel.font = [UIFont systemFontOfSize:12.0];
[appView addSubview:button]; // 给按钮添加监听方法
button.tag = i;
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
} /** 按钮监听方法 */
- (void)click:(UIButton *)button
{ NSDictionary *appData=self.appList[button.tag]; // 添加一个UILabel到界面上
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
// 数值是0表示黑色,1表示纯白
// alpha表示透明度
label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2];
label.text = appData[@"name"];
label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label];
// 动画效果
// 收尾式动画,修改对象的属性,frame,bounds,alpha
// 初始透明度,完全透明
label.alpha = 0.0; // 禁用按钮
button.enabled = NO; // 动画结束之后删除
// ^ 表示是block,块代码,是一个预先准备好的代码块,可以当做参数传递,在需要的时候执行!
// 块代码在OC中,使用的非常普遍!
[UIView animateWithDuration:1.0f animations:^{
NSLog(@"动画开始");
// 要修改的动画属性
label.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
// 动画完成后,所做的操作
NSLog(@"动画完成");
[label removeFromSuperview];
}];
}]; NSLog(@"-------");
} @end
iOS UI基础-4.0应用程序管理的更多相关文章
- iOS UI基础-4.1应用程序管理 字典转Model
用模型取代字典 使用字典的坏处 一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编辑器没有智能提示,需要手敲 dict[@"name"] = @&qu ...
- iOS UI基础-4.2应用程序管理 Xib文件使用
Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...
- iOS UI基础-17.0 UILable之NSMutableAttributedString
在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...
- iOS UI基础-16.0 UIButton
回归自然,UIButton是我们使用最频烦的一个控件.下面,对该控件的一些常用方法进行一些总结. UIButton *payStateBtn = [UIButton buttonWithType:UI ...
- iOS UI基础-13.0 数据存储
应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...
- iOS UI基础-9.0 UITableView基础
在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...
- iOS UI基础-1.0加法计算器
1.打开Xcode,新建一个项目 2.Single View Application是最适合初学者的模板 3.填写该应用相关信息 4.搭建UI界面 项目创建完毕后,自动帮我们做了很多配置,也自动生成了 ...
- iOS UI基础-19.0 UICollectionView
直接上代码,说明请看注释吧 1.继承三个代理 UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateF ...
- iOS UI基础-15.0 UIWebView
WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...
随机推荐
- 转载>>六款大数据采集平台的架构分析
随着大数据越来越被重视,数据采集的挑战变的尤为突出.今天为大家介绍几款数据采集平台: Apache Flume Fluentd Logstash Chukwa Scribe Splunk Forwar ...
- 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)
题目链接:http://ac.jobdu.com/problem.php?pid=1102 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- echarts中关于自定义legend图例文字
formatter有两种形式: - 模板 - 回调函数 模板 使用字符串模板,模板变量为图例名称 {name} formatter: 'Legend {name}' 回调函数 formatter: f ...
- vue过滤动画
一.使用<transition name="fade"></transition>标签 name="fade", 是创建个fade的类名 ...
- Unity3D 边缘高光Shader
Shader "Custom/NewShader" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...
- AES加密工具类(对称加密算法)
import java.nio.charset.Charset; import java.security.Key; import javax.crypto.Cipher;import javax.c ...
- Java、JavaWeb中单元测试用到的测试方法
写出的代码做单元测试时,一定要记住从三个方面出发:1.成功 2.异常 3 逻辑错误(即没有异常也可能程序运行出最后结果,可是呢?呵呵).这就是在做测试时我要牢记的三个方面,同时思维要严谨也即做事要 ...
- SpringBoot应用配置常用相关视图解析器
目录 SpringBoot的自动装配装配了视图解析器了吗? SpringBoot使用JSP SpringBoot中使用Thymeleaf SpringBoot中使用Freemark SpringBoo ...
- kernel中文件的读写操作可以使用vfs_read()和vfs_write
需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...
- Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP开发环境!
先安装 Apache Web服务器,终端:sudo apt-get install apache2 apache2-doc,然后测试是否安装成功.浏览器地址栏输入:http://l ...