iOS开发基础-九宫格坐标(5)
继续在iOS开发基础-九宫格坐标(4)的基础上进行优化。
一、改进思路
1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21、22行对控件属性的设置能否拿到视图类 WJQAppView ?
2) viewDidLoad 方法中第18、19行从 xib 文件中读取信息的操作能否封装到 WJQAppView ?
3) viewDidLoad 方法中第23、24行的按钮单击事件能否也放到 WJQAppView 中?
二、实例代码
重写 WjQAppView 类,其头文件内容如下,其中对外提供一个接口,数据的处理封装在实现文件中。
//WJQAppView.h
@class WJQAppInfo;
@interface WJQAppView : UIView
+ (instancetype)appInfoViewWithAppInfo:(WJQAppInfo *)appInfo;
@end
在实现文件的类扩展中,将 appxib.xib 文件中的控件重新建立连接,
//WJQAppView.m
@interface WJQAppView ()
@property (strong, nonatomic) IBOutlet UIImageView *appViewImage;
@property (strong, nonatomic) IBOutlet UILabel *appViewLabel;
@property (strong, nonatomic) IBOutlet UIButton *appViewButton;
@property (strong, nonatomic) WJQAppInfo *appInfo;
@end
实现在头文件中声明的类方法:
//WJQAppView.m
+ (instancetype)appInfoView {
NSArray *appArray = [[NSBundle mainBundle] loadNibNamed:@"appxib" owner:nil options:nil];
WJQAppView *appView = [appArray firstObject];
return appView;
} + (instancetype)appInfoViewWithAppInfo:(WJQAppInfo *)appInfo {
WJQAppView *appView = [self appInfoView];
appView.appInfo = appInfo;
return appView;
}
实现私有属性 appInfo 的 setter 方法,用 WJQAppInfo 类型参数来初始化 appViewImage 和 appViewLabel ,代码如下:
//WJQAppView.m
- (void)setAppInfo:(WJQAppInfo *)appInfo {
_appInfo = appInfo;
self.appViewImage.image = appInfo.image;
self.appViewLabel.text = appInfo.desc;
}
将 appxib.xib 中的 UIButton 建立 IBAction 单击事件,命名为 buttonClicked: ,代码如下:
//WJQAppView.m
- (IBAction)buttonClicked:(id)sender {
self.appViewButton.enabled = NO;
WJQAppInfo *appInfo = self.appInfo;
UILabel *animaLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[animaLabel setBackgroundColor:[UIColor lightGrayColor]];
[animaLabel setTextAlignment:NSTextAlignmentCenter];
animaLabel.text = [NSString stringWithFormat:@"%@已下载", appInfo.desc];
animaLabel.alpha = 0.0;
[self.superview addSubview:animaLabel];
[UIView animateWithDuration:4.0 animations:^{
//逐渐显示标签
[animaLabel setAlpha:1.0];
} completion:^(BOOL finished) {
//动画结束时,移除显示下载完成的标签
[animaLabel removeFromSuperview];
//已下载时,改变按钮标题,及背景图片
[self.appViewButton setTitle:@"下载完成" forState:UIControlStateNormal];
[self.appViewButton setBackgroundImage:[UIImage imageNamed:@"downloaded"] forState:UIControlStateNormal];
//已下载完成的,取消按钮下载触发事件
[self.appViewButton removeTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}];
}
最后,修改 ViewController 中的 viewDidLoad 方法,代码如下:
//ViewController.m
- (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坐标 WJQAppInfo *appInfo = self.apps[i];
WJQAppView *appView = [WJQAppView appInfoViewWithAppInfo:appInfo];
appView.frame = CGRectMake(appViewX, appViewY, appViewWidth, appViewHeight);
[self.view addSubview:appView];
}
}
实例代码:http://pan.baidu.com/s/1eRxsuDc
iOS开发基础-九宫格坐标(5)的更多相关文章
- iOS开发基础-九宫格坐标(6)
继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...
- iOS开发基础-九宫格坐标(4)
对iOS开发基础-九宫格坐标(3)的代码进行进一步优化. 新建一个 UIView 的子类,并命名为 WJQAppView ,将 appxib.xib 中的 UIView 对象与新建的视图类进行关联. ...
- iOS开发基础-九宫格坐标(3)之Xib
延续iOS开发基础-九宫格坐标(2)的内容,对其进行部分修改. 本部分采用 Xib 文件来创建用于显示图片的 UIView 对象. 一.简单介绍 Xib 和 storyboard 的比较: 1) X ...
- iOS开发基础-九宫格坐标(2)之模型
在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...
- iOS开发基础-九宫格坐标(1)
一.功能分析 1)以九宫格展示图片信息,每一个 UIView 包含一个 UIImageView .一个 UILabel 和一个 UIButton . 2)加载App数据,根据数据长度创建对应的格子数: ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- iOS开发——总结篇&IOS开发基础知识
IOS开发基础知识 1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断 ...
- IOS开发基础环境搭建
一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包: ...
- iOS开发基础-图片切换(4)之懒加载
延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善. 一.懒加载基本内容 懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法. 注意:懒加载时一定要先判断该 ...
随机推荐
- kubernetes学习14—Dashboard搭建和认证
本文收录在容器技术学习系列文章总目录 一.介绍 Kubernetes Dashboard是Kubernetes集群的基于Web的通用UI.它允许用户管理在群集中运行的应用程序并对其进行故障排除,以及管 ...
- selinux基本
TE模型 主体划分为若干组,称为域 客体划分为若干组,每个组称为一个类型 DDT(Domain Definition Table,域定义表,二维),表示域和类型的对应访问权限,权限包括读写执行 一 ...
- 【Angular专题】——(2)【译】Angular中的ForwardRef
原文地址:https://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html 作者:Christoph ...
- 第一个用eclipse打包APK时报错一个错误怎么解决
这个问题也是我在android开发群里面解决的一个问题. 如果有什么想法或者想法可以在下面进行评论,我们可以一起交流一下! 我们在eclipse中开发完一个程序之后,需要将其打包为APK的安装包,我们 ...
- DataTable转换成List集合,传递到HTML页面
public string GetPwd(string str) { var dt= bll.Gets(str); List<string> list = new List<stri ...
- JAVA Swing 改变标题栏左上角默认咖啡图标
前言 最近使用Java的swing开发了一个小程序,想要实现改变标题栏左上角的图标,找了网上的资料,经过了一个下午的尝试,都是未能成功,最后,终于是在Java的一本书上找到了结果 我只能说,网上的东西 ...
- Spring Boot admin 2.0 详解
一.什么是Spring Boot Admin ? Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序. 应用程序作为Spring Boot Admin C ...
- ajax点击加载更多数据图片(预加载)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript面试总结(一)
(一).call,apply,bind 的用法与区别? 答案:摘自:https://www.cnblogs.com/Jade-Liu18831/p/9580410.html(总结的特别棒的一篇文章) ...
- npm run dev 启动错误:Module build failed: Error: No PostCSS Config found in:xxxxxxxxxxxxxx
解决办法:在根目录新建postcss.config.js module.exports = { plugins: { 'autoprefixer': {browsers: 'last 5 versio ...