在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 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)
|
请发表评论