質問

I am trying to detect the touches in a UIPinchGestureRecognizer's method, because I need both the horizontal and the vertical scale. On pinch, an image should be resized accordingly. For example if the user moves the fingers only horizontally, I am resizing only the width of the view.

It is working fine, but when I take my fingers of the screen, most of the times the app crashes with SIGABRT and no message.

My code is:

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        previousHorizontalPinchDistance = ABS([recognizer locationOfTouch:1 inView:recognizer.view].x - [recognizer locationOfTouch:0 inView:recognizer.view].x);
        previousVerticalPinchDistance = ABS([recognizer locationOfTouch:1 inView:recognizer.view].y - [recognizer locationOfTouch:0 inView:recognizer.view].y);
    }
    CGFloat horizontalPinchDistance = ABS([recognizer locationOfTouch:1 inView:recognizer.view].x - [recognizer locationOfTouch:0 inView:recognizer.view].x);
    CGFloat verticalPinchDistance = ABS([recognizer locationOfTouch:1 inView:recognizer.view].y - [recognizer locationOfTouch:0 inView:recognizer.view].y);

The crash is at the line where I set the horizontalPinchDistance. Any idea why? Thanks!

役に立ちましたか?

解決

In the meantime, I've found the reason for the crash.

When the user lifts a finger, the Pinch recognizer's method still gets called (or if the user doesn't lift both fingers at the same time). The problem is that numberOfTouches becomes 1 instead of 2. So the index 1 in the method call [recognizer locationOfTouch:1 inView:recognizer.view] will be out of bounds. So the block of code in my question should only be executed if recognizer.numberOfTouches > 1.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top