Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello,
I need some help getting pinch to zoom working. In my application I handle zooming in/out differently than you regularly would in a WebView I am using Google Maps so I need to use JavaScript in order to zoom in/out.
To zoom in I use this:
Code:
id map = [aMapView windowScriptObject];
    NSString *jsCmd = @"map.zoomIn();";
    [map evaluateWebScript:jsCmd];

To zoom out I use this
Code:
id map = [aMapView windowScriptObject];
    NSString *jsCmd = @"map.zoomOut();";
    [map evaluateWebScript:jsCmd];

How can I get Pinch to Zoom working with something like this. I already read the Cocoa Event-Handling Guide for Handling Trackpad Events, but I couldn't figure out how to implement pinch to zoom when you use to different methods for zooming in and zooming out.
Thanks
 
I did use already tried that, but I wasn't sure how to use the event it passes in when you pinch to zoom.
 
After reading the magnify method you suggested I tried this but it didn't work:
Code:
-(void)magnifyWithEvent:(NSEvent *)event{

    CGFloat magnifiedChangeValue = [event magnification];
    
    if (magnifiedChangeValue <0) {
        id map = [aMapView windowScriptObject];
        NSString *jsCmd = @"map.zoomOut();";
        [map evaluateWebScript:jsCmd];

    } else {
        id map = [aMapView windowScriptObject];
        NSString *jsCmd = @"map.zoomIn();";
        [map evaluateWebScript:jsCmd];
    }
    
}
I didn't get any errors, but the map didn't zoom in or out.
 
If you put an NSLog statement in each branch of your (magnifiedChangeValue < 0) conditional, are they firing?

Can you make a test app and manually trigger the JavaScript commands by wiring it up to a simple button to see if you can get the map to change?
 
@GorillaPaws
If I use buttons to zoom in and out with that code it works. I added an NSLog to each of the if blocks and they didn't run, so the problem is handling the event. Any Ideas about why it isn't working?
 
My code is randomly working now..(sort of). I ran my project again and now it is running the NSLogs, but my map doesn't zoom in. I know that code works because it works with a button. Why doesn't the map zoom in/out?
 
Why doesn't the map zoom in/out?

Debug your code:

1. Is magnifyWithEvent: getting called every time?
2. What are the values of your variables (magnifiedChangeValue, map)?
3. What is the result of calling evaluateWebScript: ?

If the above debugging techniques all work out as expected, you may need to do some debugging within JavaScript.


BTW, even though I don't think it matters here, you can simplify your code (untested):

Code:
- (void)magnifyWithEvent:(NSEvent *)event {
    CGFloat magnifiedChangeValue = [event magnification];
    if (magnifiedChangeValue < 0) {
        [self stringByEvaluatingJavaScriptFromString:@"map.zoomOut();"];
    } else {
        [self stringByEvaluatingJavaScriptFromString:@"map.zoomIn();"];
    }
}
 
Last edited:
I think my problem is this line
Code:
CGFloat magnifiedChangeValue = [event magnification];

When I print this out in an NSLog when I pinch to zoom I get this when I zoom out I get this value: 2.12273e-314
When I zoom in I get this value
2.12434e-314

I'm using this code to print out the values:
Code:
 NSLog([NSString stringWithFormat:@"%g"],magnifiedChangeValue);
 
I'm using this code to print out the values:
Code:
 NSLog([NSString stringWithFormat:@"%g"],magnifiedChangeValue);

The square bracket is in the wrong place in your code.
Code:
NSLog([NSString stringWithFormat:@"%g", magnifiedChangeValue[COLOR=RED]][/COLOR]);

But the first parameter to NSLog is a format, so it can be simplified to:
Code:
NSLog(@"%g", magnifiedChangeValue);
 
Last edited:
@kainjow Oddly, when I used your simplified code it worked, Thanks!
Why didn't my code work?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.