• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

2048swift

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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)

      

:
  )
  :
  )
  :
  )
  :
  )
  :
 

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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift简介发布时间:2022-07-13
下一篇:
《从零开始学Swift》学习笔记(Day67)——CocoaTouch设计模式及应用之MVC模式 ...发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap