Commits

Jay Yu  committed e4f8d08

added pinch gesture recogniser. still work in progress

  • Participants
  • Parent commits bb0ca6e

Comments (0)

Files changed (3)

File Calculator/GraphingView.h

 @interface GraphingView : UIView
 
 @property (nonatomic) CGFloat scale;
-
-// - (void)pinch:(UIPinchGestureRecognizer *)gesture;
-
 @property (nonatomic, weak) id <GraphingViewDataSource> dataSource;
 
+- (void)pinch:(UIPinchGestureRecognizer *)gesture;
+
 @end

File Calculator/GraphingView.m

 @synthesize dataSource = _dataSource;
 @synthesize scale = _scale;
 
+#define DEFAULT_SCALE 1.00
+
+- (CGFloat)scale
+{
+    if (!_scale) {
+        return DEFAULT_SCALE; // don't allow zero scale
+    } else {
+        return _scale;
+    }
+}
+
+- (void)setScale:(CGFloat)scale
+{
+    if (scale != _scale) {
+        _scale = scale;
+        [self setNeedsDisplay]; // any time our scale changes, call for redraw
+    }
+}
+
+- (void)pinch:(UIPinchGestureRecognizer *)gesture
+{
+    if ((gesture.state == UIGestureRecognizerStateChanged) ||
+        (gesture.state == UIGestureRecognizerStateEnded)) {
+        self.scale *= gesture.scale; // adjust our scale
+        gesture.scale = 1;           // reset gestures scale to 1 (so future changes are incremental, not cumulative)
+    }
+}
+
 - (void)drawRect:(CGRect)rect
 {
     CGContextRef context = UIGraphicsGetCurrentContext();
     midPoint.x = self.bounds.origin.x + self.bounds.size.width / 2;
     midPoint.y = self.bounds.origin.y + self.bounds.size.height / 2;
 
-    CGFloat scale = 1.000;  // TODO: Get from gesture recogniser / etc
+    CGFloat scale = self.scale;
 
     [AxesDrawer drawAxesInRect:rect originAtPoint:midPoint scale:scale];
     
-    CGFloat x = 0.0;
-    CGFloat y = 0.0;
-    double x_t;  // Converted from CGFloat to the graph coordination system
-    double y_t;
+    CGFloat x = 0.0;  // Always begin drawing from the left edge of the UIView
+    CGFloat y;
+    double x_t;  // X Axis UIView coordinate - converted from CGFloat to the graph coordination system
+    double y_t;  // Raw Y Axis value returned from runProgram
     BOOL drawing = NO;
     
     while (x < self.bounds.size.width) {
             CGContextAddLineToPoint(context, x, y);
         }
         drawing = YES;
-        x = x + (1.000 / self.contentScaleFactor);
+        x = x + (1.0 / self.contentScaleFactor);  // Support hi-res drawing on Retina devices
     }
     [[UIColor blueColor] setStroke];
     CGContextDrawPath(context, kCGPathStroke);
                            originAtPoint:(CGPoint)midPoint
                                    scale:(CGFloat)scale
 {
-    CGFloat val = x - midPoint.x;
-    val = val * scale;
-    
+    CGFloat val = x - midPoint.x * scale;
     return (double) val;
 }
 
                            originAtPoint:(CGPoint)midPoint
                                    scale:(CGFloat)scale
 {
-    CGFloat val = midPoint.y - (CGFloat) y;
-    val = val * scale;
-    
-    return val;
+    CGFloat val = (CGFloat) y / scale;
+    return midPoint.y - val;
 }
 
 - (void)setup

File Calculator/GraphingViewController.m

 {
     _graphingView = graphingView;
     self.graphingView.dataSource = self;
+
+    // enable pinch gestures in the FaceView using its pinch: handler
+    [self.graphingView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(pinch:)]];
 }
 
 - (void)setup