Handling Magnification Events

Discussion in 'Mac Programming' started by Blakeasd, Jul 16, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You could try subclassing WebView and overriding magnifyWithEvent:
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I did use already tried that, but I wasn't sure how to use the event it passes in when you pinch to zoom.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Take a look at NSEvent's magnification method.
     
  5. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    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.
     
  6. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #6
    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?
     
  7. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #7
    @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?
     
  8. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #8
    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?
     
  9. kainjow, Jul 17, 2011
    Last edited: Jul 17, 2011

    kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    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();"];
        }
    }
     
  10. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #10
    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);
    
     
  11. jiminaus, Jul 17, 2011
    Last edited: Jul 17, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    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);
     
  12. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #12
    @kainjow Oddly, when I used your simplified code it worked, Thanks!
    Why didn't my code work?
     

Share This Page