How do I zoom in/out of a canvas with pinch gesture?

  1. 6 days ago

    I am currently trying to make an image viewer and I want to know if there's a way to zoom in/out of a canvas picture with two fingers pinching the picture like the regular Photo viewer in iOS.

    Any help would be appreciated.

  2. Jason K

    Nov 13 Pre-Release Testers

    Use a pinch gesture recognizer from iOSKit and then create a CGAffineTransform and apply it to your image view. The following is Obj-C but should translate easily with iOSKit.

    -(void)HandlePinch:(UIPinchGestureRecognizer*)recognizer{
        if ([recognizer state] == UIGestureRecognizerStateEnded) {
            NSLog(@"======== Scale Applied ===========");
            if ([recognizer scale]<1.0f) {
                [recognizer setScale:1.0f];
            }
            CGAffineTransform transform = CGAffineTransformMakeScale([recognizer scale],  [recognizer scale]);
            imgView.transform = transform;
        }
    }
  3. Jérémie L

    Nov 13 Pre-Release Testers, Xojo Pro Europe (France)
    Edited 6 days ago

    By default image views do not receive gestures (same with labels)

    From the docs.

    Image views ignore user events by default. Normally, you use image views only to present visual content in your interface. If you want an image view to handle user interactions as well, change the value of its userInteractionEnabled property to YES

    This should help

     declare sub userInteractionEnabled_ lib uikitlib selector "setUserInteractionEnabled:" (obj_id as ptr, value as boolean)
    userInteractionEnabled_(imgView.handle, true)
  4. I am fairly new to XOJO declares. I have the iOSKit setup, but I am not sure how to use the UIPinchGesture class.

    Do call the method Extensions.AddGestureRecognizer(myView, myUIPinchGesture) inside a canvas?

    My current attempt is to use the iOSGesture class, but the scale parameter given inside the PinchChanged EventHandler is giving me incorrect scale values. It would go into a digit that is in the millions.

or Sign Up to reply!