複利ってこんな感じなのねって言う収束感を試すiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SpriteKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
weak var scene : SKScene?
weak var table : UITableView?
override func viewDidLoad() {
super.viewDidLoad()
self.setupScene()
self.createHundred()
self.createInterestTable()
self.createBank()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.table?.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: false, scrollPosition: .Top)
}
func setupScene() {
var size = self.view.bounds.size
var spriteView = SKView(frame: CGRect(x: 0, y: 0, width: size.width * 0.7, height: size.height))
self.view.addSubview(spriteView)
var scene = SKScene(size: spriteView.bounds.size)
scene.backgroundColor = SKColor(hue: 0.5, saturation: 0.7, brightness: 1, alpha: 1)
spriteView.presentScene(scene)
self.scene = scene
}
func createHundred() {
var size = self.view.bounds.size
var coin = SKShapeNode(circleOfRadius: 40)
coin.name = “coin”
coin.position = CGPointMake(size.width * 0.35, 50)
coin.fillColor = UIColor(hue: 0.18, saturation: 0.8, brightness: 0.8, alpha: 1)
self.scene?.addChild(coin)
var label = SKLabelNode(text: “100”)
label.fontSize = 20
label.fontName = “Superclarendon-Black”
label.verticalAlignmentMode = .Center
coin.addChild(label)
}
func createBank() {
var size = self.view.bounds.size
var building = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(150, 150))
building.position = CGPoint(x: size.width * 0.35, y: size.height * 0.6)
self.scene?.addChild(building)
for i in 0..<3 {
var window = SKSpriteNode(color: UIColor.lightGrayColor(), size: CGSizeMake(30, 30))
window.position = CGPoint(x: i * 40 – 40 , y: 40.0)
building.addChild(window)
}
var label = SKLabelNode(text: “BANK”)
label.position = CGPoint(x: 0, y: –30)
label.fontColor = UIColor.darkGrayColor()
building.addChild(label)
var label2 = SKLabelNode(text: “Rate 0.5”)
label2.position = CGPoint(x: 35, y: –70)
label2.fontSize = 18
label2.fontColor = UIColor.blackColor()
building.addChild(label2)
}
var mlist : Array<String> = [“1. yearly”,“12. monthly”, “52. weekly”, “365. daily”]
func createInterestTable() {
var size = self.view.bounds.size
var table = UITableView(frame: CGRectMake(size.width*0.7, 0, size.width * 0.3, size.height))
table.backgroundColor = UIColor(hue: 0.8, saturation: 0.1, brightness: 1, alpha: 1)
table.dataSource = self
table.delegate = self
self.view.addSubview(table)
self.table = table
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mlist.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return CGRectGetMaxY(tableView.bounds) / 4.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(“cell”) as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: “cell”)
cell?.contentView.backgroundColor = UIColor(hue: 0.85, saturation: 0.1, brightness: 1, alpha: 1)
cell?.textLabel?.text = mlist[indexPath.row]
}
return cell!
}
func calculateCompoundInterest() -> Double {
var type = mlist[self.table!.indexPathForSelectedRow()!.row]
var interest : NSString = type.componentsSeparatedByString(“.”)[0]
var rate = 0.05
var result = 100.0 * pow(1.0 + rate/interest.doubleValue, interest.doubleValue)
return result;
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var p = touches.anyObject()?.locationInNode(self.scene)
if let coin = self.scene?.childNodeWithName(“coin”) {
coin.runAction(SKAction.sequence([
SKAction.moveByX(0, y:80, duration: 0.5),
SKAction.fadeOutWithDuration(0.2),
SKAction.runBlock({ () -> Void in
let l = coin.children[0] as SKLabelNode
l.text = NSString(format: “%.2f”,self.calculateCompoundInterest())
}),
SKAction.fadeInWithDuration(0.2),
SKAction.moveByX(0, y:-80, duration: 0.5),
SKAction.waitForDuration(2.0),
SKAction.runBlock({ () -> Void in
dispatch_async(dispatch_get_main_queue(), { coin.removeFromParent()
self.createHundred()
})
})
]))
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}