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 UIKitclass 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 UIKitclass 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 UIKitclass 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的页面添加服务器端代码的另一 ...
随机推荐
- Sort List 分类: leetcode 算法 2015-07-10 15:35 1人阅读 评论(0) 收藏
对链表进行排序,要求时间复杂度为O(n log n) ,不使用额外的空间. 我一开始的想法是借助quicksort的思想,代码如下: # time O(nlog(n)) # Definition fo ...
- 介绍 32 位和 64 位版本的 Microsoft Office 2010
在使用 64 位版本的 Office 2010 运行现有解决方案时存在两个基本问题: Office 2010 中的本机 64 位进程无法加载 32 位二进制文件.在使用现有 Microsoft Act ...
- oracle 11g 11.2.0.1 设置HuagePage导致TRC 变大 变多
最近发现diag/..../trac/ 目录下 sid_ora_xxxx.trc 文件大小为11M 而且类似文件数量很大.导致占用了8G硬盘空间 另外个同事说他的DG没有这个问题. 都一样的系统和一 ...
- haproxy 配置日志
jrhppt01:/root# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1 glo ...
- Jump的计划
欢迎訪问我的github:https://github.com/xdnm 1.熟悉cocos2dx2.2.3开发框架 a.熟悉cocos2d api ...
- Action的返回值类型总结
Action的返回值 MVC 中的 ActionResult是其他所有Action返回类型的基类,下面是我总结的返回类型,以相应的帮助方法: 下面是这些方法使用的更详细的例子 一.返回View ...
- 《WCF技术剖析》博文系列汇总[持续更新中]
原文:<WCF技术剖析>博文系列汇总[持续更新中] 近半年以来,一直忙于我的第一本WCF专著<WCF技术剖析(卷1)>的写作,一直无暇管理自己的Blog.在<WCF技术剖 ...
- js中使用jstl中得到的值
jstl的标签会转化为服务器端的代码执行,而js代码则在客户端执行. 要在js中使用jstl并不是直接将jstl的value赋值给一个js的变量,而是要在jstl的value上加上&qu ...
- 谁知道哪有比较好的Beijing Milonga?想去参加这样的阿根廷探戈舞会~
谁知道哪有比较好的Beijing Milonga?想去参加这样的阿根廷探戈舞会~_百度知道 谁知道哪有比较好的Beijing Milonga?想去参加这样的阿根廷探戈舞会~ 2009-1 ...
- [Codecademy] HTML&CSS第八课:Design a Button for Your Webwite
本文出自 http://blog.csdn.net/shuangde800 [Codecademy] HTML && CSS课程学习目录 --------------------- ...