iOS開發UI篇——九宮格座標計算

來源:互聯網
上載者:User

標籤:

一、要求

     完成下面的布局

二、分析

     尋找左邊的規律,每一個uiview的x座標和y座標。

三、實現思路

 

    (1)明確每一塊用得是什麼view

    (2)明確每個view之間的父子關係,每個視圖都只有一個父視圖,擁有很多的子視圖。

    (3)可以先嘗試逐個的添加格子,最後考慮使用for迴圈,完成所有uiview的建立

    (4)載入app資料,根據資料長度建立對應個數的格子

    (5)添加格子內部的子控制項

    (6)給內部的子控制項裝配資料

四、程式碼範例

////  YYViewController.m//  九宮格練習////  Created by 孔醫己 on 14-5-22.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *apps;@end@implementation YYViewController//1.載入資料- (NSArray *)apps{    if (!_apps) {        NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];        _apps=[NSArray arrayWithContentsOfFile:path];    }    return _apps;}- (void)viewDidLoad{    [super viewDidLoad];    NSLog(@"%d",self.apps.count);        //2.完成布局設計        //三列    int totalloc=3;    CGFloat appvieww=80;    CGFloat appviewh=90;        CGFloat margin=(self.view.frame.size.width-totalloc*appvieww)/(totalloc+1);    int count=self.apps.count;    for (int i=0; i<count; i++) {        int row=i/totalloc;//行號        //1/3=0,2/3=0,3/3=1;        int loc=i%totalloc;//列號                CGFloat appviewx=margin+(margin+appvieww)*loc;        CGFloat appviewy=margin+(margin+appviewh)*row;                        //建立uiview控制項        UIView *appview=[[UIView alloc]initWithFrame:CGRectMake(appviewx, appviewy, appvieww, appviewh)];        //[appview setBackgroundColor:[UIColor purpleColor]];        [self.view addSubview:appview];                        //建立uiview控制項中的子視圖        UIImageView *appimageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];        UIImage *appimage=[UIImage imageNamed:self.apps[i][@"icon"]];        appimageview.image=appimage;        [appimageview setContentMode:UIViewContentModeScaleAspectFit];       // NSLog(@"%@",self.apps[i][@"icon"]);        [appview addSubview:appimageview];                //建立文字標籤        UILabel *applable=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 80, 20)];        [applable setText:self.apps[i][@"name"]];        [applable setTextAlignment:NSTextAlignmentCenter];        [applable setFont:[UIFont systemFontOfSize:12.0]];        [appview addSubview:applable];                //建立按鈕        UIButton *appbtn=[UIButton buttonWithType:UIButtonTypeCustom];        appbtn.frame= CGRectMake(10, 70, 60, 20);        [appbtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];        [appbtn setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];        [appbtn setTitle:@"下載" forState:UIControlStateNormal];        appbtn.titleLabel.font=[UIFont systemFontOfSize:12.0];        [appview addSubview:appbtn];                [appbtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];    }}-(void)click{    //動畫標籤    UILabel *animalab=[[UILabel alloc]initWithFrame:CGRectMake(self.view.center.x-100, self.view.center.y+20, 200, 40)];    [animalab setText:@"下載成功"];    animalab.font=[UIFont systemFontOfSize:12.0];    [animalab setBackgroundColor:[UIColor brownColor]];    [animalab setAlpha:0];    [self.view addSubview:animalab];    //    [UIView beginAnimations:Nil context:Nil];//    [animalab setAlpha:1];//    [UIView setAnimationDuration:4.0];//    [UIView commitAnimations];        //執行完之後,還得把這給刪除了,推薦使用block動畫        [UIView animateWithDuration:4.0 animations:^{    [animalab setAlpha:1];    } completion:^(BOOL finished) {        //[self.view re];    }];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@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.