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 4177: Mike的农场( 最小割 )

    显然是最小割... 对于规律(i, j, k) i,j 互相连边, 容量为k 对于规则(S, a, b) 新建一个点x, x与S中每个点连一条弧, 容量+∞, 然后再根据a决定x与源点或汇点连边. 跑 ...

  2. Mars的mp3实例

    Mars的mp3实例第一课: 关于menu: package mars.mp3player01; import mars.down.HttpDownloader; import android.app ...

  3. 【转】CentOS上安装 jdk:rpm安装和源码安装

    1.安装 jdk-8u5-linux-x64.rpm 原文链接:http://www.cnblogs.com/xsi640/p/3756995.html 先下载最新的jdk版本 文件名:jdk-8u5 ...

  4. Python之路day4

    坚持就是胜利.今天零下14度,从教室出来的路上真的很冷很冷,希望这个冬天自己不会白过,春暖花开的时候一定要给世界一个更好的自己. 原本以为day3的作业自己做得挺好的,没想到只得了B+.必须要加油了, ...

  5. 一天一个类,一点也不累之HashSet

    最近忙着一个小项目结题,故没能按时完成[一天一个类,一点也不累],还好项目优秀,算是对自己一点点的安慰和鼓励.~~~ 今天要说的是HashSet 既然是继承自Set,那么就必须有Set的一些属性,比如 ...

  6. ETC_百度百科

    ETC_百度百科 ETC(电子不停车收费系统)

  7. 王立平-- ContentValues , HashTable , HashMap差别

    ContentValues  :是一种存储机制,key-value 特点:key仅仅能是string类型.value:仅仅能是基本类型,不能是对象. 应用:经常使用语往数据库中插入数据 Content ...

  8. 安卓面试精华(Activity部分)

    过几天小弟要去面试了,当然免不了要好好复习下功课,其实很多东西也不是特别清楚,今天都当作一个回顾和巩固,希望我的这篇文章能对即将去找工作的同学有所帮助. 1. Q:什么是activity? 虽然这个问 ...

  9. 【Demo 0009】Android 组件(BroadcastReceiver)

    本章学习要点:        1.  了解Broadcast的作用;        2.  掌握自定义广播和系统广播的接收:        3.  掌握广播的发送:

  10. 上一篇括号配对让人联想起catalan数,顺便转载一篇归纳的还不错的文章

    转载请注明来自souldak,微博:@evagle 怎么样才是合法的组合? 只要每一时刻保证左括号的数目>=右括号的数目即可. 直接递归就行,每次递归加一个括号,左括号只要还有就能加,右括号要保 ...