iOS UI基礎 九宮格演算法

來源:互聯網
上載者:User

標籤:blog   http   color   io   os   ar   strong   for   檔案   

 

 

案例分析:

分析:

 

 

 

 

代碼案例實現:

//

//  ViewController.m

//  01-九宮格演算法

//

//  Created by FlyLee on 14-9-12.

//  Copyright (c) 2014年 FLYLEE.CN. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

/*

 資料容器

 */

@property (nonatomic,strong) NSArray *apps;

 

@end

 

@implementation ViewController

 

//懶載入

- (NSArray *)apps

{

    if (_apps == nil) {

        

        // 1.獲得plist檔案的全路徑

        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];

        

        // 2.載入數組

        _apps = [NSArray arrayWithContentsOfFile:path];

        

    }

    return _apps;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    //1. 每一行的列數

    int totalCol = 3;

    

    //2.每一個格子的尺寸及y值的常量

    CGFloat appW = 85;

    CGFloat appH = 90;

    CGFloat startY = 30; //設定Y的初始位置,電池欄20 所以初始位置設定30

    CGFloat marginY = 15; //設定Y的初始間隙空間

    

    //3.間隙 = (控制器view的寬頻 - 3 *格子的寬度 ) / 4;

    CGFloat marginX = (self.view.frame.size.width - totalCol * appW) / (totalCol + 1);

    int count = self.apps.count;

    

    //4.根據應用管理的個數,建立格子

    for (int i = 0 ; i < count;  i++) {

        //4.1 建立view

        UIView *appView = [[UIView alloc] init];

        appView.backgroundColor = [UIColor redColor];

        

        //4.2 計算行號和列號

        // i = 0, 1 ,2 / 3 = 0    初始的都是0行開始  0 , 1 ,  2  ,3 ....

        // i = 3, 4 ,5 / 3 = 1

        

        int row = i / totalCol;

        

        // i  =  0 , 3, 6 % 3 = 0 初始的都是0列開始  0 , 1 ,  2  ,3 ....

        // i  =  1, 4 , 7 %3 =  1

        

        int col = i % totalCol;

        

//        4.3  計算x和y

        CGFloat appX = marginX + (appW + marginX) * col;

        CGFloat appY = startY + (appH + marginY) * row;

        

        appView.frame = CGRectMake(appX,appY, appW,appH);

        

        //4.4 添加格子到控制器的view

        [self.view addSubview:appView];

        

    

        

    }

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 

iOS UI基礎 九宮格演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.