Swift - 使用UIView给页面添加4×4方格
1,下面是一个利用UIView来给页面上绘制灰色方块的例子,效果图如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
import UIKit class ViewController : UIViewController { //游戏方格维度 var dimension: Int = 4 //数字格子的宽度 var width: CGFloat = 50 //格子与格子的间距 var padding: CGFloat = 6 //保存背景图数据 var backgrounds: Array < UIView >! override func viewDidLoad() { super .viewDidLoad() self .backgrounds = Array < UIView >() //改成主视图背景白色背景 self .view.backgroundColor = UIColor .whiteColor() setupGameMap() } func setupGameMap() { var x: CGFloat = 50 var y: CGFloat = 150 for i in 0..<dimension { println (i) y = 150 for j in 0..<dimension { //初始化视图 var background = UIView (frame: CGRectMake (x, y, width, width)) background.backgroundColor = UIColor .darkGrayColor() self .view.addSubview(background) //将视图保存起来,以备后用 backgrounds.append(background) y += padding + width } x += padding+width } } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
2,进阶版 - 继承UIView实现自定义方块组件(有颜色和数字)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import UIKit class TileView : UIView { //颜色映射表,不同的数字颜色不同 let colorMap = [ 2: UIColor .redColor(), 4: UIColor .orangeColor(), 8: UIColor .yellowColor(), 16: UIColor .greenColor(), 32: UIColor .brownColor(), 64: UIColor .blueColor(), 128: UIColor .purpleColor(), 256: UIColor .cyanColor(), 512: UIColor .lightGrayColor(), 1024: UIColor .magentaColor(), 2048: UIColor .blackColor() ] //在设置值时,更新视图的背景和文字 var value: Int = 0{ didSet { backgroundColor = colorMap[value] numberLabel.text= "\(value)" } } var numberLabel: UILabel ! //初始化视图 init (pos: CGPoint , width: CGFloat , value: Int ) { numberLabel = UILabel (frame: CGRectMake (0,0, width, width)) numberLabel.textColor = UIColor .whiteColor() numberLabel.textAlignment = NSTextAlignment . Center numberLabel.minimumScaleFactor = 0.5 numberLabel.font = UIFont (name: "微软雅黑" , size:20) numberLabel.text = "\(value)" super . init (frame: CGRectMake (pos.x, pos.y, width, width)) addSubview(numberLabel) self .value = value backgroundColor = colorMap[value] } required init (coder aDecoder: NSCoder ) { super . init (coder : aDecoder) } } |
使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import UIKit class ViewController : UIViewController { //游戏方格维度 var dimension: Int = 4 //数字格子的宽度 var width: CGFloat = 50 //格子与格子的间距 var padding: CGFloat = 6 //保存背景图数据 var backgrounds: Array < TileView >! override func viewDidLoad() { super .viewDidLoad() self .backgrounds = Array < TileView >() //改成主视图背景白色背景 self .view.backgroundColor = UIColor .whiteColor() setupGameMap() } func setupGameMap() { var x: CGFloat = 50 var y: CGFloat = 150 for i in 0..<dimension { println (i) y = 150 for j in 0..<dimension { //随机2的1~11次方 var val: Int = 2<< Int (arc4random_uniform(10)) //初始化视图 var background = TileView (pos: CGPoint (x:x,y:y), width: self .width, value: val) self .view.addSubview(background) //将视图保存起来,以备后用 backgrounds.append(background) y += padding + width } x += padding+width } } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
Swift - 使用UIView给页面添加4×4方格的更多相关文章
- swift为UIView添加extension扩展frame
添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...
- swift关于UIView设置frame值的extension
swift关于UIView设置frame值的extension 使用 说明 1. 使用如上图,很简单,不再赘述 2. 在extension给添加的计算属性提供getter,setter方法即可 源码 ...
- 在Magento System Configuration页面添加配置项
以 Jp_Coupon 模块为例: 目标: 在 System configuration 页面添加一个 JP tab, 在JP中添加 Coupon section, 然后给 Coupon sectio ...
- 如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试
本文转载自:http://www.cnblogs.com/pmars/p/3673811.html 先看效果图: 以下是原文: 如何在帮助页面添加测试工具 上一篇我在ASP.NET里面添加了一个Hel ...
- 如何给你的ASP.NET页面添加HelpPage
如何给你的ASP.NET页面添加HelpPage 最近写了一些webAPI,所以需要搞一套API的帮助文档,google了一下,发现这是可以自动生成的,以下就是如何自动生成HelpPage的说明. 参 ...
- Magento给新产品页面添加分页
本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过 ...
- 如何给magento的产品页面添加返回按钮
如何给magento的产品页面添加返回按钮,最模板提供教程 第一步: 打开 E:\xampp\htdocs\magento\skin\frontend\default\bluescale\css\st ...
- sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page
转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...
- 向SharePoint页面添加后台代码
转:http://www.cnblogs.com/chenzehe/archive/2009/12/25/1631863.html 在本文中,我将跟大家一起讨论,为MOSS的页面添加服务器端代码的另一 ...
随机推荐
- Nmon 性能:分析 AIX 和 Linux 性能的免费工具
原文摘自: http://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 官网:http://www.ibm.com/developerw ...
- Case of the Zeros and Ones
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Andrew ...
- 网页制作之JavaScript部分 1 - 语法(复制教材内容)
一.简介 1.JavaScript它是个什么东西? 它是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 2.它与Java有什么关系? 没有什么直接联系,java是Sun公司(已经没有了,被O ...
- 简单描述RAID级别:
简单描述RAID级别: RAID 0 是俩盘一起读写,如果一个坏了那么数据全丢失: RAID 1是一块写,一块用来备份,坏一块无所谓: RAID 2 ,3 ,4 不常用: 最常用的就是RAID 5和R ...
- STM32启动模式
STM32三种启动模式对应的存储介质均是芯片内置的,它们是: 1)用户闪存 = 芯片内置的Flash.2)SRAM = 芯片内置的RAM区,就是内存啦.3)系统存储器 = 芯片内部一块特定的区域,芯片 ...
- C++内存管理(超长)
[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...
- ARM过程调用标准---APCS简单介绍
介绍 APCS,ARM 过程调用标准(ARM Procedure Call Standard),提供了紧凑的编写例程的一种机制,定义的例程能够与其它例程交织在一起.最显著的一点是对这些例程来自哪里没有 ...
- Bandwidth内存带宽測试工具
本博文为原创,遵循CC3.0协议,转载请注明出处:http://blog.csdn.net/lux_veritas/article/details/24766015 ----------------- ...
- Caffe —— Deep learning in Practice
因工作交接须要. 要将caffe用法及总体结构描写叙述清楚. 鉴于也有同学问过我相关内容, 决定在本文中写个简单的tutorial, 方便大家參考. 本文简单的讲几个事情: Caffe能做什么? 为什 ...
- Oracle_Database_11g_标准版_企业版__下载地址_详细列表
Oracle_Database_11g_标准版_企业版__下载地址_详细列表 Oracle Database 11g Release 2 Standard Edition and Enterprise ...