Commit b8e55e85 authored by toby's avatar toby

show colors in detail view

parent e3cbae5e
......@@ -9,14 +9,83 @@
import UIKit
class ColorDetailViewController: UIViewController {
class ColorCell: UITableViewCell {
@IBOutlet var labelTitle: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ColorDetailViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
private let colorCellIdentifier = "ColorCell"
var colors = [UIColor]()
override func viewWillAppear(animated: Bool) {
navigationController!.setNavigationBarHidden(false, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.redColor()
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(ColorCell.self, forCellReuseIdentifier: colorCellIdentifier)
}
private func labelForColor(color: UIColor) -> String {
var label = ""
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
red = floor(red * 255)
green = floor(green * 255)
blue = floor(blue * 255)
label += "#" + (NSString(format: "%2X", Int(red)) as String)
label += (NSString(format: "%2X", Int(green)) as String)
label += (NSString(format: "%2X", Int(blue)) as String)
label = label.stringByReplacingOccurrencesOfString(" ", withString: "0", options: .LiteralSearch, range: nil)
return label
}
private func imageFromColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0, 0, 35, 20)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return colors.count
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(colorCellIdentifier, forIndexPath: indexPath) as? UITableViewCell
let color = colors[indexPath.row]
cell!.textLabel!.text = labelForColor(color)
cell!.imageView!.image = imageFromColor(color)
return cell!
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ import UIKit
enum BackgroundState:StateMachineDataSource{
case Ready, AnimatingBackground, AddBlock(UIView), RemoveBlock(UIView), PausedBackground, PresentColorDetail, Finished
case Ready, AnimatingBackground, AddBlock(UIView), RemoveBlock(UIView), PausedBackground, Finished
func shouldTransitionFrom(from:BackgroundState, to: BackgroundState) -> Should {
switch (from, to){
......@@ -42,8 +42,6 @@ enum BackgroundState:StateMachineDataSource{
return .Continue
case(.AddBlock(let block), .PausedBackground):
return .Continue
case(_, .PresentColorDetail):
return .Continue
default:
return .Abort
}
......@@ -71,8 +69,6 @@ extension WorldViewController: StateMachineDelegate {
case ((let originState), .AddBlock(let block)):
addBlock(block)
machine.state = originState
case (_, .PresentColorDetail):
println("segue color detail")
default:
break
}
......@@ -281,7 +277,13 @@ class WorldViewController: UIViewController {
}
@IBAction func flipIt(sender: AnyObject) {
machine.state = .PresentColorDetail
let colorDetail = ColorDetailViewController()
var colors = [UIColor]()
colors = blocks.map({ return $0.backgroundColor! })
colors += barriers.map({ return $0.backgroundColor! })
colors.append(background.backgroundColor!)
colorDetail.colors = colors
navigationController!.pushViewController(colorDetail, animated: true)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment