-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphViewController.swift
73 lines (65 loc) · 2.25 KB
/
GraphViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// GraphViewController.swift
// Calculator
//
// Created by Caroline Liu on 2015-08-26.
// Copyright (c) 2015 Caroline Liu. All rights reserved.
//
import UIKit
class GraphViewController: UIViewController, GraphViewDataSource {
@IBOutlet weak var graphView: GraphView! {
didSet {
graphView.dataSource = self
graphView.addGestureRecognizer(UIPinchGestureRecognizer(target: graphView, action: "scale:") )
}
}
@IBAction func reposition(gesture: UITapGestureRecognizer) {
switch gesture.state {
case .Ended: fallthrough
case .Changed:
let location = gesture.locationInView(view)
graphView.center = CGPoint(x: location.x, y: location.y )
default: break
}
}
@IBAction func move(gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .Ended: fallthrough
case .Changed:
let translation = gesture.translationInView(graphView)
graphView.center = CGPoint(x:graphView.center.x + translation.x,
y:graphView.center.y + translation.y)
default: break
}
gesture.setTranslation(CGPointZero, inView: self.view)
}
/* the Model
*/
var brain: CalculatorBrain? {
didSet {
updateUI()
}
}
private func updateUI() {
graphView?.setNeedsDisplay()
}
/*
interpret the model for the view
walk through all values in the range of X that are currently in the view
replace the M value for each X to get the Y value
*/
func pointsForGraph(sender: GraphView) -> [CGPoint] {
var points = [CGPoint]()
for var x = sender.bounds.minX; x <= sender.bounds.maxX; x++ {
// need to re-calculate x given the bounds should be within an equal range of -x to +x
var xval = (x - sender.center.x) / sender.scale
brain?.variableValues["M"] = Double(xval)
// TODO: When the graphView gets manipulated by a gesture we may hav to re-calculate this?
if let result = brain?.evaluate() {
var yval = CGFloat(result)
points.append(CGPoint(x: xval, y: yval))
}
}
return points
}
}