2048 swift
AppearanceProvider.swift
import UIKit
protocol AppearanceProviderProtocol:class {
func tileColor(value:Int)->UIColor
func numberColor(value:Int) -> UIColor
func fontForNumbers()->UIFont
}
class AppearanceProvider:AppearanceProviderProtocol {
func tileColor(value:Int)->UIColor {
switch value {
case 2:
return UIColor(red:238.0/255.0,green:228.0/255.0,blue:218.0/255.0,alpha:1.0)
case 4: | |
return UIColor(red: 237.0/255.0, green: 224.0/255.0, blue: 200.0/255.0, alpha: 1.0) | |
case 8: | |
return UIColor(red: 242.0/255.0, green: 177.0/255.0, blue: 121.0/255.0, alpha: 1.0) | |
case 16: | |
return UIColor(red: 245.0/255.0, green: 149.0/255.0, blue: 99.0/255.0, alpha: 1.0) | |
case 32: | |
return UIColor(red: 246.0/255.0, green: 124.0/255.0, blue: 95.0/255.0, alpha: 1.0) | |
case 64: | |
return UIColor(red: 246.0/255.0, green: 94.0/255.0, blue: 59.0/255.0, alpha: 1.0) |
case 128,256,1024,2048:
return UIColor(red:237.0/255.0,green:207.0/255.0,blue:114.0/255.0,alpha:1.0)
default:
return UIColor.whiteColor
}
}
}
func numberColor(value:Int) -> UIColor {
switch value {
case 2,4:
return UIColor(red:119.0/255.0,green:110.0/255.0,101.0/255.0,alpha:1.0)
default:
return UIColor.whiteColor()
}
func fontFotNumbers()->UIFont {
return UIFont(name:"HelveticaNenu-Bold",size:20)
}
}
}
a class implements a protocol use : good interface
UIColor() UIFont() font name, how to use ,later study
tileColor, numberColor, fontForNumbers
import UIKit
class ViewController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startGameButtonTapped(sender:UIButton) {
let game = NumberTileGameViewController(dimension:4,threshold:2048)
self.presentViewController(game,animated:true,completion:nil)
}
}
import UIKit
class TileView:UIView {
var delegate:AppearanceProviderProtocol
var value:Int = 0 {
didSet{
backgroundColor=delegate.tileColor(value)
numberLabel.textColor = delegate.numberColor(value)
numberLabel.text = "\(value)"
}
}
var numberLabel:UILabel
required init(coder:NSCoder) {
fatalError("NSCoding not supported")
}
init(position:CGPoint, width:CGFloat, value:Int, radius:CGFloat,delegate d:AppearanceProviderProtocol) {
delegate = d
numberLabel = UILabel(frame:CGReckMake(0,0,width,width))
numberLabel.textAlignment = NSTExtAlignment.Center
numberLabel.minimumScaleFactor = 0.5
numberLabel.font = delegate.fontForNumbers()
super.init(frame:CGRectMake(position.x,position.y,width,width))
addSubview(numberLabel)
layer.cornerRadius = radius
self.value = value
backgroundColor = delegate.tileColr(value)
numberLabel.textColor = delegate.numberColor(value)
numberLabel.text = "\(value)"
}
}
TileView MVC View , implement protocol AppearanceProvicerProtol
didSet super.init
import UIKit
class GameboardView:UIView {
var dimension:Int
var tileWidth:CGFloat
var titlePadding:CGFloat
var cornerRadius:CGFloat
var tiles:Dictionary(NSIndexPath,TileView)
let provider = AppearanceProvider()
let tilePopStartScale:CGFloat:0.1
let tilePopMaxScale:CGFloat:1.1
let tilePopDelay:NSTimeInterval=0.05
let tileExpandTime:NSTimeInterval:0.18
let tileContractTime:NStimeInterval:0.08
let tileMergeStartScale:CGFloat=1.0
let tileMergeExpandTIme:NSTImeInterval = 0.08
let tileMergeContractTime:NSTimeINterval = 0.08
let perSquareSlideDuration:NSTimeInterval = 0.08.
init(dimension d:Int, tileWidth width:CGFLoat, tilePadding padding:CGFloat, cornerRadius radius:CGFloat, backgorund)
{
assert(d>0)
dimension=d
tileWidth = width
tilePadding = padding
cornerRedius = radius
tiles = Dictionary()
let sideLength = padding + CGFloat(dimension)*(width+padding)
super.init(frame:CGRectMake(0,0,sideLength,sideLength))
layer.cornerRadius = radius
setupBackground(backgroundColor:backgroundColor,tileColor:foregroundCOlor)
}
required init(coder:NSCoder) {
fatalError("NSCoding not supported")
}
func reset() {
for(key,tile) in tiles {
tile.removeFromSuperView()
}
tiles.removeAll(keepCapacity:true)
}
func positionIsValid(pos:(Int,Int))->Bool {
let(x,y) = pos
return (x>=0 && x < dimension && y >=0 && y < dimension)
}
func setupBackground(backgroundColor bgColor:UIColor, tileColor:UIColor) {
backgroundColor = bgColor
var xCursor = tilePadding
var yCursor:CGFloat
let bgRadius = (cornerRadius >-2)?cornerRadius-2:0
for i in 0..<dimension {
yCursor = tilePadding
for j in 0..<dimmesion
let bakcground= UIView(frame:CGRectMake(xCUrsor,yCursor,tileWidth,tileWidth))
background.layer.cornerRadius
background.backgrohndCOlor =
}
}
func insertTile(pos:(Int, Int),value:Int) {
assert(positionIsValid(pos))
let(row,col) = pos
let x = tilePadding + CGFloat(col) *(tileWidth + tilePadding)
let y = tilePadding + CGFloat(row)*(tileWidth + tilePadding)
let r = (cornerRidus > = 2) ?cornerRadius - 2:0
let tile = TileView(position:CGPointMake(x,y),width:tileWidth,value:value,radius:r,delegate:provider)
tile.layer.setAffineTransform(CGAffineTransformMakeScale(tilePopStartScale,tilePopStartScale)
addSubView(tile)
bringSubViewToFont(tile)
tiles[NSIndexPath(forRow:row,inSection:col)] = tile
UIView.animateWithDuration(tileExpandTime,delay:tilePopDelay,options:UIViewAnimationOPtions.TransitionNone,
animations:{ ()-> Void in
tile.layer.setAffineTransform(CGAffineTransformMakeScale(self.tilePopMaxScale,self.titlePopMaxScale))
} ,
completion:{(finshed:Bool)->Void in
UIView.animateWithDuration(self.tileContractTIme,animations:{()->Void in
tile.layer.setAffineTransform(CGAffineTransformIdentity)
})
}
)
}
}
Dictionary(NSIndexPath, TileView)
2048 swift的更多相关文章
- Swift实战之2048小游戏
上周在图书馆借了一本Swift语言实战入门,入个门玩一玩^_^正好这本书的后面有一个2048小游戏的实例,笔者跟着实战了一把. 差不多一周的时间,到今天,游戏的基本功能已基本实现,细节我已不打算继续完 ...
- Swift开发实例:苹果Swift编程语言新手教程中文版+FlappyBird,2048游戏源代码
源代码: 用IOS Swift语言实现的Flappy Bird源代码:http://download.csdn.net/detail/estellise/7449547 用IOS Swift实现的游戏 ...
- [swift实战入门]手把手教你编写2048(一)
苹果设备越来越普及,拿着个手机就想捣鼓点啥,于是乎就有了这个系列,会一步一步教大家学习swift编程,学会自己做一个自己的app,github地址:https://github.com/scarlet ...
- swift 的枚举、结构体、类
一.Swift的枚举 枚举是一系相关联的值定义的一个公共的组类型,同时能够让你在编程的时候在类型安全的情况下去使用这些值.Swift中的枚举比OC中的枚举强大得多, 因为Swift中的枚举是一等类型, ...
- Swift编程语言资料合集
在本周二凌晨召开的苹果年度开发者大会WWDC上,苹果公司推出了全新的编程语言Swift.Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提 ...
- 12套swift学习资源分享
虽然objective-c编程语言在过去很长一段时间都是iOS应用开发的基础语言,且很多iOS开发者对其也深爱有佳,但是随着swift编程语言的问世,迅速发展为开发者追捧的语言.且今年伴随着swift ...
- 21个高质量的Swift开源iOS App
原文:21 Amazing Open Source iOS Apps Written in Swift 对Swift初学者来说,学习开源项目,阅读源码是个不错的方法.在这篇文章中,基于对代码质量和排名 ...
- Swift初学习
距离swift发布10天了,也简单看了一下swift的语法,个人感觉相对于object-c很是简单明了.Swift的出现并不能说明iOS开发简单了很多,有可能会变得复杂,你需要学习两门编程语言,因为在 ...
- iOS - Swift String 字符串
前言 public struct String public class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCodin ...
随机推荐
- Java RTTI和反射
一.Java的RTTI RTTI(Run-Time Type Identification,通过运行时类型识别)的含义就是在运行时识别一个对象的类型,其对应的类是Class对象,每个java里面的类 ...
- Solr开发参考文档(转)
Solr开发文档 Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇文章中,将介绍 Solr 并展示如何轻松地将其表现优异的全文本搜索 ...
- 【每日一linux命令8】添加新的工作组(groupadd)
groupadd (字意add group)增加一个新的工作组. 语法:groupadd 选项 用户组名 选项: -g 指定新建工作组的ID -r 创建系统工作组,系统工作组的ID小于500 -k 覆 ...
- Spring Boot 入门概念介绍
使用spring-boot快速开发spring应用 转 http://itindex.net/detail/49108-spring-boot-%E5%BC%80%E5%8F%91 spring多年以 ...
- java 吞吐量
jvm中 ,执行用户的代码占 的时间/总时间 ,假如前者是99 分钟,后者一分钟,则吞吐量为99% ,吞吐量越大.系统越好,如何设计系统,导致系统吞吐量高,因为我们知道,垃圾回收,7种垃圾收集器,也不 ...
- javascript、js操作json方法总结(json字符创转换json对象)
相信前端的同学们对json并不陌生,接触过很多.但是很少人知道json的全称是什么,哈哈,我也是查资 料知道的.(JSON JavaScript Object Notation是一种轻量级的数据交换格 ...
- .net自定义控件
一.[.net自定义控件]TextBox控件重写 之NumTextBox 参考博客:http://www.yongfa365.com/Item/NumTextBox.html 二.[.net自定义控件 ...
- 关于这两天研究Java打印pdf方法的记录
这两天在研究Java调用打印机打印PDF文件的方法,学到了不少东西,特别来记录一下. 关于Java打印网上最多的而且也是Java正统的打印方法就是使用PrintService,一套比較标准的打印代码例 ...
- docker入门(二)
打造自己的镜像 首先我们启动busybox镜像为容器,在该容器中安装一个小工具,再将这个容器保存为新的镜像 首先我们下载一个镜像,再启动容器 [root@centos ~]# docker pull ...
- [转] ubuntu 12.04 安装 nginx+php+mysql web服务器
Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...