Jay Yu avatar Jay Yu committed 512115d

finally got the pinch zooming right
added pan gesture

Comments (0)

Files changed (3)

Calculator/GraphingView.h

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

Calculator/GraphingView.m

 #import "GraphingView.h"
 #import "AxesDrawer.h"
 
+
 @implementation GraphingView
 @synthesize dataSource = _dataSource;
 @synthesize scale = _scale;
+@synthesize origin = _origin;
 
 #define DEFAULT_SCALE 1.00
 
     }
 }
 
+- (CGPoint)origin
+{
+    if (!_origin.x && !_origin.y) {
+        _origin = CGPointMake(self.bounds.origin.x + self.bounds.size.width / 2, self.bounds.origin.y + self.bounds.size.height / 2);
+    }
+    return _origin;
+}
+
+- (void)setOrigin:(CGPoint)origin
+{
+    if (origin.x != _origin.x && origin.y != _origin.y) {
+        _origin = origin;
+        [self setNeedsDisplay]; // any time our scale changes, call for redraw
+    }
+}
+
 - (void)pinch:(UIPinchGestureRecognizer *)gesture
 {
     if ((gesture.state == UIGestureRecognizerStateChanged) ||
     }
 }
 
+- (void)pan:(UIPanGestureRecognizer *)gesture
+{
+    if ((gesture.state == UIGestureRecognizerStateChanged) ||
+        (gesture.state == UIGestureRecognizerStateEnded)) {
+        CGPoint translation = [gesture translationInView:self];
+        self.origin = CGPointMake(self.origin.x + translation.x, self.origin.y + translation.y);
+        [gesture setTranslation:CGPointZero inView:self];
+    }
+}
+
+
 - (void)drawRect:(CGRect)rect
 {
     CGContextRef context = UIGraphicsGetCurrentContext();
     
-    // TODO: triple tap will be able to reset this centre thing
-    CGPoint midPoint; // center of our bounds in our coordinate system
-    midPoint.x = self.bounds.origin.x + self.bounds.size.width / 2;
-    midPoint.y = self.bounds.origin.y + self.bounds.size.height / 2;
+    CGPoint midPoint = self.origin; // center of our bounds in our coordinate system
 
     CGFloat scale = self.scale;
 
             CGContextMoveToPoint(context, x, y);
         } else {
             CGContextAddLineToPoint(context, x, y);
+// Use this if clause to debug
+//            if ((midPoint.x - x) > -10.00 && (midPoint.x - x) < 10.00) {
+//                NSLog(@"Drawing: x: %f x_t: %f y: %f y_t: %f at scale %f", x, x_t, y, y_t, self.scale);
+//            }
         }
         drawing = YES;
         x = x + (1.0 / self.contentScaleFactor);  // Support hi-res drawing on Retina devices
                            originAtPoint:(CGPoint)midPoint
                                    scale:(CGFloat)scale
 {
-    CGFloat val = x - midPoint.x * scale;
+    CGFloat val = (x - midPoint.x) / scale;
     return (double) val;
 }
 
                            originAtPoint:(CGPoint)midPoint
                                    scale:(CGFloat)scale
 {
-    CGFloat val = (CGFloat) y / scale;
+    CGFloat val = (CGFloat) y * scale;
     return midPoint.y - val;
 }
 

Calculator/GraphingViewController.m

 
     // enable pinch gestures in the FaceView using its pinch: handler
     [self.graphingView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(pinch:)]];
+
+    [self.graphingView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self.graphingView action:@selector(pan:)]];
 }
 
 - (void)setup
                           [NSNumber numberWithDouble:xAxis], @"x", nil];
     
     double yAxis = [CalculatorBrain runProgram:self.program usingVariableValues:vars];
-    NSLog(@"yAxis: x axis: %f, y axis: %f", xAxis, yAxis);
+//    NSLog(@"yAxis: x axis: %f, y axis: %f", xAxis, yAxis);
     
     return yAxis;
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.