iOS開發基礎-九宮格座標(5)

  繼續在iOS開發基礎-九宮格座標(4)的基礎上進行優化。html

1、改進思路

  1)iOS開發基礎-九宮格座標(4)中 viewDidLoad 方法中的第2一、22行對控件屬性的設置可否拿到視圖類 WJQAppView ?app

  2) viewDidLoad 方法中第1八、19行從 xib 文件中讀取信息的操做可否封裝到 WJQAppView ?ide

  3) viewDidLoad 方法中第2三、24行的按鈕單擊事件可否也放到 WJQAppView 中?優化

 

2、實例代碼

  重寫 WjQAppView 類,其頭文件內容以下,其中對外提供一個接口,數據的處理封裝在實現文件中。動畫

1 //WJQAppView.h
2 @class WJQAppInfo;
3 @interface WJQAppView : UIView
4 + (instancetype)appInfoViewWithAppInfo:(WJQAppInfo *)appInfo;
5 @end

  在實現文件的類擴展中,將 appxib.xib 文件中的控件從新創建鏈接,atom

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

  實如今頭文件中聲明的類方法:spa

 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 ,代碼以下:code

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: ,代碼以下:orm

 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 方法,代碼以下:htm

 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 }

 

參考博客:iOS開發UI篇—從代碼的逐步優化看MVC

實例代碼:http://pan.baidu.com/s/1eRxsuDc

相關文章
相關標籤/搜索