功能与界面

功能分析:

  1. 以九宫格的形式展示应用信息
  2. 点击下载按钮后,做出相应的操作
步骤分析:
  1. 加载应用信息
  2. 根据应用的个数创建对应的view
  3. 监听下载按钮点击

整个应用界面:

程序实现

思路

  1. UI布局:N宫格
  2. 事件监听
  3. 动态添加 (by plist)
  4. 整体封装,组合每个应用信息,使用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应用程序管理的更多相关文章

  1. iOS UI基础-4.1应用程序管理 字典转Model

    用模型取代字典 使用字典的坏处 一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编辑器没有智能提示,需要手敲 dict[@"name"] = @&qu ...

  2. iOS UI基础-4.2应用程序管理 Xib文件使用

    Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...

  3. iOS UI基础-17.0 UILable之NSMutableAttributedString

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

  4. iOS UI基础-16.0 UIButton

    回归自然,UIButton是我们使用最频烦的一个控件.下面,对该控件的一些常用方法进行一些总结. UIButton *payStateBtn = [UIButton buttonWithType:UI ...

  5. iOS UI基础-13.0 数据存储

    应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...

  6. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  7. iOS UI基础-1.0加法计算器

    1.打开Xcode,新建一个项目 2.Single View Application是最适合初学者的模板 3.填写该应用相关信息 4.搭建UI界面 项目创建完毕后,自动帮我们做了很多配置,也自动生成了 ...

  8. iOS UI基础-19.0 UICollectionView

    直接上代码,说明请看注释吧 1.继承三个代理 UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateF ...

  9. iOS UI基础-15.0 UIWebView

    WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...

随机推荐

  1. 分页SQL代码

    city_id ORDER BY city_id )

  2. Elasticsearch学习之深入聚合分析一---基本概念

    首先明白两个核心概念:bucket和metric 1. bucket:一个数据分组 city name 北京 小李 北京 小王 上海 小张 上海 小丽 上海 小陈 基于city划分buckets,划分 ...

  3. C语言程序设计--字符串与指针及数组与指针

    数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...

  4. 编译源码 JAVA out of memory

  5. Pry的安装

    Pry 用于rails应用的调试 在Gemfile中添加 gem 'pry', :group =>:development bundle install 即可.pry代替irb方法,直接运行: ...

  6. jQuery:find()方法与children()方法的区别

    1:children及find方法都用是用来获得element的子elements的,两者都不会返回 text node,就像大多数的jQuery方法一样. 2:children方法获得的仅仅是元素一 ...

  7. Html5游戏框架createJs组件--EaselJS(二)绘图类graphics

    有端友问我是否有文档,有确实有,但没有中文的,只有英文的,先提供浏览地址供大家参考学习createJs英文文档.                        EaselJS其实主要就是createJ ...

  8. 【转】UTF16和UTF8什么区别?

    这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级.整理这篇文章的动机是两个问题: 问题一: 使用Windows记事本的“另存为 ...

  9. vim自定义语法高亮(syntax highlight)设置流程

    这里用一个非常简单的例子来展示vim自定义自己的语法并高亮显示的设置流程. 所使用的实例语言是python,为了区分vim自带的python语法,我们把文件名保存为test.me,其内容如下图所示: ...

  10. iOS ViewControllers 瘦身

    https://objccn.io/issue-1-1/ https://juejin.im/user/57ddfba4128fe10064cbb93a 把 Data Source 和其他 Proto ...