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];
}
}

参考博客:iOS开发UI篇—从代码的逐步优化看MVC

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

上一篇:从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能


下一篇:从头认识java-15.7 Map(4)-介绍HashMap的工作原理-hash碰撞(常常作为面试题)