iOS开发基础-九宫格坐标(5)
继续在iOS开发基础-九宫格坐标(4)的基础上进行优化。
一、改进思路
1)iOS开发基础-九宫格坐标(4)中 viewDidLoad 方法中的第21、22行对控件属性的设置能否拿到视图类 WJQAppView ?
2) viewDidLoad 方法中第18、19行从 xib 文件中读取信息的操作能否封装到 WJQAppView ?
3) viewDidLoad 方法中第23、24行的按钮单击事件能否也放到 WJQAppView 中?
二、实例代码
重写 WjQAppView 类,其头文件内容如下,其中对外提供一个接口,数据的处理封装在实现文件中。
1 //WJQAppView.h 2 @class WJQAppInfo; 3 @interface WJQAppView : UIView 4 + (instancetype)appInfoViewWithAppInfo:(WJQAppInfo *)appInfo; 5 @end
在实现文件的类扩展中,将 appxib.xib 文件中的控件重新建立连接,
1 //WJQAppView.m 2 @interface WJQAppView () 3 @property (strong, nonatomic) IBOutlet UIImageView *appViewImage; 4 @property (strong, nonatomic) IBOutlet UILabel *appViewLabel; 5 @property (strong, nonatomic) IBOutlet UIButton *appViewButton; 6 @property (strong, nonatomic) WJQAppInfo *appInfo; 7 @end
实现在头文件中声明的类方法:
1 //WJQAppView.m 2 + (instancetype)appInfoView { 3 NSArray *appArray = [[NSBundle mainBundle] loadNibNamed:@"appxib" owner:nil options:nil]; 4 WJQAppView *appView = [appArray firstObject]; 5 return appView; 6 } 7 8 + (instancetype)appInfoViewWithAppInfo:(WJQAppInfo *)appInfo { 9 WJQAppView *appView = [self appInfoView]; 10 appView.appInfo = appInfo; 11 return appView; 12 }
实现私有属性 appInfo 的 setter 方法,用 WJQAppInfo 类型参数来初始化 appViewImage 和 appViewLabel ,代码如下:
1 //WJQAppView.m 2 - (void)setAppInfo:(WJQAppInfo *)appInfo { 3 _appInfo = appInfo; 4 self.appViewImage.image = appInfo.image; 5 self.appViewLabel.text = appInfo.desc; 6 }
将 appxib.xib 中的 UIButton 建立 IBAction 单击事件,命名为 buttonClicked: ,代码如下:
1 //WJQAppView.m 2 - (IBAction)buttonClicked:(id)sender { 3 self.appViewButton.enabled = NO; 4 WJQAppInfo *appInfo = self.appInfo; 5 UILabel *animaLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 450, 200, 20)]; 6 [animaLabel setBackgroundColor:[UIColor lightGrayColor]]; 7 [animaLabel setTextAlignment:NSTextAlignmentCenter]; 8 animaLabel.text = [NSString stringWithFormat:@"%@已下载", appInfo.desc]; 9 animaLabel.alpha = 0.0; 10 [self.superview addSubview:animaLabel]; 11 [UIView animateWithDuration:4.0 animations:^{ 12 //逐渐显示标签 13 [animaLabel setAlpha:1.0]; 14 } completion:^(BOOL finished) { 15 //动画结束时,移除显示下载完成的标签 16 [animaLabel removeFromSuperview]; 17 //已下载时,改变按钮标题,及背景图片 18 [self.appViewButton setTitle:@"下载完成" forState:UIControlStateNormal]; 19 [self.appViewButton setBackgroundImage:[UIImage imageNamed:@"downloaded"] forState:UIControlStateNormal]; 20 //已下载完成的,取消按钮下载触发事件 21 [self.appViewButton removeTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 22 }]; 23 }
最后,修改 ViewController 中的 viewDidLoad 方法,代码如下:
1 //ViewController.m 2 - (void)viewDidLoad { 3 [super viewDidLoad]; 4 5 int totalColumn = 3; //3列 6 CGFloat margin = (self.view.frame.size.width - totalColumn*appViewWidth) / (totalColumn + 1); 7 int count = self.apps.count; 8 NSLog(@"%d", count); 9 10 for (int i = 0; i < count; i++) { 11 int row = i/totalColumn; //行号,从0开始 12 int column = i%totalColumn; //列号,从0开始 13 CGFloat appViewX = margin + (margin + appViewWidth) * column; //子视图的X坐标 14 CGFloat appViewY = margin + (margin + appViewHeight) * row; //子视图的Y坐标 15 16 WJQAppInfo *appInfo = self.apps[i]; 17 WJQAppView *appView = [WJQAppView appInfoViewWithAppInfo:appInfo]; 18 appView.frame = CGRectMake(appViewX, appViewY, appViewWidth, appViewHeight); 19 [self.view addSubview:appView]; 20 } 21 }