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实现自定义方块组件(有颜色和数字)

  
方块组件:TileView.swift
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方格的更多相关文章

  1. swift为UIView添加extension扩展frame

    添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...

  2. swift关于UIView设置frame值的extension

    swift关于UIView设置frame值的extension 使用 说明 1. 使用如上图,很简单,不再赘述 2. 在extension给添加的计算属性提供getter,setter方法即可 源码 ...

  3. 在Magento System Configuration页面添加配置项

    以 Jp_Coupon 模块为例: 目标: 在 System configuration 页面添加一个 JP tab, 在JP中添加 Coupon section, 然后给 Coupon sectio ...

  4. 如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试

    本文转载自:http://www.cnblogs.com/pmars/p/3673811.html 先看效果图: 以下是原文: 如何在帮助页面添加测试工具 上一篇我在ASP.NET里面添加了一个Hel ...

  5. 如何给你的ASP.NET页面添加HelpPage

    如何给你的ASP.NET页面添加HelpPage 最近写了一些webAPI,所以需要搞一套API的帮助文档,google了一下,发现这是可以自动生成的,以下就是如何自动生成HelpPage的说明. 参 ...

  6. Magento给新产品页面添加分页

    本文介绍如何让magento创建一个带分页功能的新到产品页面,方便我们在首页或者其它CMS Page调用和展示新到产品. 在Magento我们经常有的做法是建立一个可以调用新产品的block,然后通过 ...

  7. 如何给magento的产品页面添加返回按钮

    如何给magento的产品页面添加返回按钮,最模板提供教程 第一步: 打开 E:\xampp\htdocs\magento\skin\frontend\default\bluescale\css\st ...

  8. sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page

    转:http://blog.csdn.net/chenxinxian/article/details/8720893 在sharepoint 2010的页面中,我们发现,没有页尾,如果我们需要给页面添 ...

  9. 向SharePoint页面添加后台代码

    转:http://www.cnblogs.com/chenzehe/archive/2009/12/25/1631863.html 在本文中,我将跟大家一起讨论,为MOSS的页面添加服务器端代码的另一 ...

随机推荐

  1. BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )

    用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...

  2. 深入windows的关机消息截获-从XP到Win7的变化

    之前写了一个软件用于实验室的打卡提醒,其中一个重要的功能是在关机之前提醒当天晚上是否已经打卡.之前我是在WM_ENDSESSION中弹出一个模态对话框来提醒,在XP中基本工作正常,在Win7中大多数时 ...

  3. [置顶] getline函数-linux

    头文件: #include <stdio.h> 函数: ssize_t getline(char **lineptr, size_t *n, FILE *stream); eg: ssiz ...

  4. Tri_integral Summer Training 9 总结

    比赛链接 A B C D H I J K 多灾多难的 Summer Training 9,前一天挂了一场比赛,结果题一半不能做,于是打了一个小时就放弃了.之后的两场Summer Training 9一 ...

  5. Login oracle for external authenticate

    Generally, we can login the oracle by os authentication, if we login os in a remote machine and make ...

  6. html5游戏开发--"动静"结合用地图块拼成大地图 & 初探lufyl

    一.前言   本次教程将向大家讲解如何用html5将小地图块拼成大地图,以及如何用现有的高级html5游戏开发库件lufylegend.js开发游戏.   首先让我们来了解了解如何用html5实现动画 ...

  7. hdu1172猜数字

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1172 题目 猜数字 Time Limit: 20000/10000 MS (Java/Others) ...

  8. 数据结构读书笔记(三)(C语言)

    栈 顺序实现: 存储结构: #define STACK_INIT_SIZE 10 // 存储空间初始分配量 #define STACK_INCREMENT 2 // 存储空间分配增量 struct S ...

  9. Java基础12 类型转换与多态

    链接地址:http://www.cnblogs.com/vamei/archive/2013/04/01/2992662.html 作者:Vamei 出处:http://www.cnblogs.com ...

  10. Linux权限操作 [转]

    Linux权限操作 本文内容来自<鸟哥linux私房菜>读后个人做的笔记,该书实为学习linux的很好入门教材 一.文件属性 ls ls -al列出所有的档案属性 ls是List的意思 档 ...