iOS开发 -------- 九宫格坐标计算

时间:2023-02-12 18:17:36

一 要求


完成下面的布局

iOS开发 -------- 九宫格坐标计算

二 分析


寻找规律,每一个UIView的x坐标和y坐标

iOS开发 -------- 九宫格坐标计算

三 实现思路


(1) 明确每一块用得是什么View;

(2) 明确每个View之间的父子关系,每个视图都只有一个父视图,拥有很多的子视图;

(3) 可以先尝试逐个的添加格子,最后考虑使用for循环,完成所有的UIView创建;

(4) 加载app数据,根据数据长度创建对应个数的格子;

(5) 添加格子内部的子控件

(6) 给内部的子控件装配数据

四 代码示例


 //
// RootViewController.m
// 九宫格坐标计算
//
// Created by lovestarfish on 15/11/1.
// Copyright © 2015年 S&G. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()
//* 数据源数组 /
@property (nonatomic,retain) NSArray *apps; @end @implementation RootViewController - (NSArray *)apps {
//1. 加载数据
if (!_apps) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
self.apps = dic[@"app"];
}
return _apps;
} - (void)viewDidLoad {
[super viewDidLoad]; self.title = @"九宫格展示"; //2. 完成布局设计
//三列
int totalloc = ;
CGFloat appViewWidth = ;
CGFloat appViewHeight = ;
CGFloat margin = (self.view.frame.size.width- totalloc * appViewWidth) / (totalloc + );
int appCount = (int)self.apps.count;
for (int i = ; i < appCount; i++) {
int row = i / totalloc;//行号
int loc = i % totalloc;//列号
CGFloat appViewX = margin + (margin + appViewWidth) * loc;
CGFloat appViewY = margin + (margin + appViewHeight) * row;
//创建UIView控件
UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(appViewX, appViewY + , appViewWidth, appViewHeight)];
[self.view addSubview:appView];
[appView release]; //创建UIView控件中的子视图
UIImageView *appImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
appImageView.image = [UIImage imageNamed:self.apps[i][@"image"]];
appImageView.contentMode = UIViewContentModeScaleAspectFit;
[appView addSubview:appImageView];
[appImageView release]; //创建文本标签
UILabel *appLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
appLabel.text = self.apps[i][@"name"];
appLabel.textAlignment = NSTextAlignmentCenter;
appLabel.font = [UIFont systemFontOfSize:];
[appView addSubview:appLabel];
[appLabel release]; //创建按钮
UIButton *appButton = [UIButton buttonWithType:UIButtonTypeSystem];
appButton.frame = CGRectMake(, , , );
[appButton setTitle:@"下载" forState:UIControlStateNormal];
appButton.backgroundColor = [UIColor greenColor];
appButton.titleLabel.font = [UIFont systemFontOfSize:];
[appButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[appView addSubview:appButton];
[appButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
} /**
* 下载按钮的响应事件
*/
- (void)click {
//动画标签
UILabel *animaLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x - , self.view.center.y - + , , )];
animaLabel.text = @"下载成功";
animaLabel.font = [UIFont systemFontOfSize:];
animaLabel.textAlignment = NSTextAlignmentCenter;
animaLabel.backgroundColor = [UIColor brownColor];
animaLabel.alpha = ;
[self.view addSubview:animaLabel];
[animaLabel release]; //执行完之后,还得把这给删除了,推荐使用block动画
[UIView animateWithDuration:4.0 animations:^{
animaLabel.alpha = 1.0;
} completion:^(BOOL finished) {
[animaLabel removeFromSuperview];
}]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
} @end

五 执行效果


iOS开发 -------- 九宫格坐标计算iOS开发 -------- 九宫格坐标计算

 

iOS开发 -------- 九宫格坐标计算的更多相关文章

  1. iOS开发UI篇—九宫格坐标计算

    iOS开发UI篇—九宫格坐标计算 一.要求 完成下面的布局 二.分析 寻找左边的规律,每一个uiview的x坐标和y坐标. 三.实现思路 (1)明确每一块用得是什么view (2)明确每个view之间 ...

  2. iOS开发UI篇——九宫格坐标计算

    一.要求 完成下面的布局 二.分析 寻找左边的规律,每一个uiview的x坐标和y坐标. 三.实现思路 (1)明确每一块用得是什么view (2)明确每个view之间的父子关系,每个视图都只有一个父视 ...

  3. iOS开发 百度坐标点的计算

    BMKMapPoint point1 = BMKMapPointForCoordinate(_userGps); BMKMapPoint point2 = BMKMapPointForCoordina ...

  4. iOS开发小技巧--计算label的Size的方法总结

    计算label的Size方法 sizeWithAttributes:方法 适用于不换行的情况,宽度不受限制的情况 /// 根据指定文本和字体计算尺寸 - (CGSize)sizeWithText:(N ...

  5. iOS开发 火星坐标转百度坐标

    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(latitude, longitude);//原始坐标 //转换 google地图.s ...

  6. iOS开发 百度坐标转火星坐标

    - (CLLocationCoordinate2D)hhTrans_GCGPS:(CLLocationCoordinate2D)baiduGps { const double x_pi = 3.141 ...

  7. iOS开发基础-九宫格坐标&lpar;1&rpar;

    一.功能分析 1)以九宫格展示图片信息,每一个 UIView 包含一个 UIImageView .一个 UILabel 和一个 UIButton . 2)加载App数据,根据数据长度创建对应的格子数: ...

  8. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

  9. iOS开发基础-九宫格坐标&lpar;6&rpar;

    继续对iOS开发基础-九宫格坐标(5)中的代码进行优化. 优化思路:把字典转模型部分的数据处理操作也拿到模型类中去实现,即将 ViewController 类实现中 apps 方法搬到 WJQAppI ...

随机推荐

  1. if &lowbar;&lowbar;name&lowbar;&lowbar;&equals;&equals; &quot&semi;&lowbar;&lowbar;main&lowbar;&lowbar;&quot&semi; 的意思&lpar;作用&rpar;python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

  2. word20161210

    gateway / 网关 gateway account / 网关帐户 Gateway Service for NetWare / NetWare 网关服务 GDI objects / GDI 对象 ...

  3. Elasticsearch--配置文件

    config目录下有2个配置文件:es的配置文件:elasticsearch.yml日志配置文件:logging.yml,更多内容请参考:ELK教程 cluster.name: elasticsear ...

  4. Java基本开发环境搭建&lpar;适合第一次使用&rpar;

    Java基本开发环境搭建(适合第一次使用) 编写人:cc 阿爸 2013-10-17 一.开发工具获取 1.开发工具包JDK l  下载地址: 到ORACLE公司官方网站(http://www.ora ...

  5. RAC 环境下修改归档模式

    RAC环境下的归档模式切换与单实例稍有不同,主要是共享存储所产生的差异.在这种情况下,我们可以将RAC数据库切换到非集群状态下,仅仅在一个实例上来实施归档模式切换即可完成RAC数据库的归档模式转换问题 ...

  6. FZU 1856 The Troop &lpar;JAVA高精度)

    Problem 1856 The Troop Accept: 72    Submit: 245Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...

  7. ADT SDK Manager启动时一闪而过

    原因为使用了Android Studio的绿色JRE,必须要安装安装版JDK或者JRE,绿色版JRE放在ADT目录虽然能启动ADT但是不能启动SDK Manager

  8. 继承 in her it

    ''' in her it 继承 de rive 派生 python2 (经典类|新式类) python3 (新式类) 1. What is inheritance? 什么是继承? 继承是一种新建类的 ...

  9. P4238 【模板】多项式求逆

    思路 多项式求逆就是对于一个多项式\(A(x)\),求一个多项式\(B(x)\),使得\(A(x)B(x) \equiv 1 \ (mod x^n)\) 假设现在多项式只有一项,显然\(B(x)\)的 ...

  10. &lbrack;HEOI2016&sol;TJOI2016&rsqb;字符串(后缀数组&plus;二分&plus;主席树&sol;后缀自动机&plus;倍增&plus;线段树合并)

    后缀数组解法: 先二分最长前缀长度 \(len\),然后从 \(rnk[c]\) 向左右二分 \(l\) 和 \(r\) 使 \([l,r]\) 的 \(height\geq len\),然后在主席树 ...