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 ...
随机推荐
- PHP中统计目录中文件以及目录中目录的大小
<?php #循环遍历目录中所有的文件,并统计目录和文件的大小 $dirName="phpMyAdmin"; $dir=opendir($dirName); #返回一 ...
- struts.enable.DynamicMethodInvocation = true 动态方法调用(转)
原文地址:http://blog.csdn.net/wfcaven/article/details/5937557 default.properties 在Struts 2的核心jar包-struts ...
- 三星I9308(移动版)正确Root的方法,进入正确的recovery的关键(网上很多方法是误导)
三星I9308(移动版)正确Root的方法,进入正确的recovery的关键(网上很多方法是误导) 1)首先在电脑上安装手机驱动:下载地址:点击这里下载 2)手机设置USB调试 方法1:设置- ...
- 关于uploadify 没有显示进度条!!!!
如果你也弄了很久不知道为什么不出现上传进度条!,那就一定要看这里了! 我注释了 queueID 属性后 就出现了!!!!! 就是这么简答! //添加界面的附件管理 $('#file_upload'). ...
- VI文件编辑操作说明
vi Hello.c (回车后就进入,按i或a键开始编辑.要退出按ESC,进入中间模式,按冒号 :后面跟命令):wq (保存并退出):q!(退出不保存)
- windowIsTranlucent 属性
项目中踩的大坑. 先埋. int alwaysFinish = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERS ...
- CompareValidator ASP控件
定义和用法 CompareValidator 控件用于将由用户输入到输入控件的值与输入到其他输入控件的值或常数值进行比较. 注释:如果输入控件为空,则不会调用任何验证函数,并且验证将成功.使用 Req ...
- [技术翻译]Guava-libraries(一): 用户指导
用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...
- Java并发编程之CAS
CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术.简单来说,比较和替换是使用一个期望值和一个变量的当前值进行比较,如果当前变量的值与我们期望的值相等,就使用一个新值替 ...
- SOA与EBS集成_Package 调用维护
介绍SOA Suite与EBS集成成,使用PACKAGE调用集成的组合程序的运维方法: 1.打开组合程序,在oracle adapter上右键,选择edit,在配置向导中跳转到第五步,确认组合程序调用 ...