10

I am making a 2D puzzle for iOS and currently I`m trying to implement zooming and scrolling with UIPinchGestureRecognizer.

Zooming is done this way: I have a target 2D vector which is a "zooming point". The code is:

glTranslatef(target.x, target.y, 0);
glScalef(scale, scale, 0);
glTranslatef(-target.x, -target.y, 0);

Target is being selected with gesture recognizer this way:

-(void)handlePinchGesture:(UIPinchGestureRecognizer*)recognizer
{
    if (UIGestureRecognizerStateBegan == [recognizer state])
    {
        view->setTarget([recognizer locationInView:self]);
    }
    // Rest of the code omitted
}

Everything works just fine.

Initially the game was designed for iPAD, but I want it to work on iPhone and iPOD too. But iPhone and iPOD have different aspect ratio. To keep initial picture proportions I decided to make initial Y scale a bit bigger. Also this made possible to swipe the game field up and down with initial zoom factor. The code is:

glTranslatef(target.x, target.y, 0);
glScalef(scale, scale * aspectRatio, 0);
glTranslatef(-target.x, -target.y, 0);

This works just fine IF the game field is "centered" at the screen(e.g. when there are equal space in bot swipe directions(up and down)). But if we swipe the field up or down and begin pinch gesture, the game field jumps to the centre again.

I understand that I need to translate the gesture position by some offset, but I cannot figure how exactly for 3 days.

riens
  • 101
  • 2
  • I didn't exactly get your zooming and moving code, but it sounds like you don't take in account the new position of your scene after the move and by that zoom with the old position as zoom center. You need to remember in which position your scene currently is and concat scaling and translating. – Kai Huppmann Apr 19 '12 at 15:31
  • I don`t handle scene position - only scale "target", which can be anywhere on the screen and cannot be relied on :( – riens Apr 20 '12 at 12:07
  • Rather find and recalibrate the function "glOrthof" then scaling with some aspect ratio.. – Matic Oblak May 17 '12 at 13:38
  • Have you considered using Cocos2D? – Idan Oct 05 '12 at 12:21
  • One suggestion would be to overlay a UIScrollView/UIView on your GL view (no content) and use it to interpret pinch/scroll... You can use it to provide native feeling scrolling/zooming. I think this practice was recommended at the last WWDC. – nielsbot Nov 07 '12 at 07:30

1 Answers1

1

Save the amount of translation you do at swipe, then after each call to handlePinchGesture, apply the translation which you had saved at swipe.

I hope it helps.

Daniel
  • 23,129
  • 12
  • 109
  • 154
Usman.3D
  • 1,791
  • 3
  • 16
  • 27