how to interpret "scale"

In the Developer Documentation "UIKit-touches presses gestures-UIPinchGestureRecognizier-var: scale". In the section of Discussion, it said"You may set the scale factor, but doing so resets the velocity......Apply the scale value to the state of the view when the gesture is first recognized—do not concatenate the value each time the handler is called." What does it exactly mean?Something about "incremental" "exponential"?

Answered by Claude31 in 410783022

If I remember well, scale must be reset in .changed.

That is the same situation as for pan gestures.


Could you try making distinction between the 2 cases:


@objc func adjustFaceCardScale(byHandlingGestureRecognizerBy recognizer: UIPinchGestureRecognizer){
        switch recognizer.state {
          case .changed:
              faceCardScale *= recognizer.scale
              recognizer.scale = 1.0
         case .ended:
              faceCardScale *= recognizer.scale
              // recognizer.scale = 1.0
          default:
              break
        }
    }



Make a few test, commenting out:

- neither line 05 nor line 08 : that should work as it works now

- both line 05 and line 08 : that should NOT work as it does not work now

- only line 08 : my assumption is that it works

- only line 05 : my assumption is that it does not work

Not sure to understand your question.


But the advice is clear: just apply scale in began state, not in changed.


Doing in change may effectively have undetermined effects (such as exponential scaling, as scale value would be applied to the existing new size.

Actually, the reason that makes me to focus on "scale" is about "gesture recognizer handler".


@objc func adjustFaceCardScale(byHandlingGestureRecognizerBy recognizer: UIPinchGestureRecognizer){

switch recognizer.state {

case .changed,.ended:

faceCardScale *= recognizer.scale

recognizer.scale = 1.0

default:

break

}

}


If I get rid of "recognizer.scale = 1.0". The image size of faceCard will change rapidly, not as expected. How it works?

Accepted Answer

If I remember well, scale must be reset in .changed.

That is the same situation as for pan gestures.


Could you try making distinction between the 2 cases:


@objc func adjustFaceCardScale(byHandlingGestureRecognizerBy recognizer: UIPinchGestureRecognizer){
        switch recognizer.state {
          case .changed:
              faceCardScale *= recognizer.scale
              recognizer.scale = 1.0
         case .ended:
              faceCardScale *= recognizer.scale
              // recognizer.scale = 1.0
          default:
              break
        }
    }



Make a few test, commenting out:

- neither line 05 nor line 08 : that should work as it works now

- both line 05 and line 08 : that should NOT work as it does not work now

- only line 08 : my assumption is that it works

- only line 05 : my assumption is that it does not work

"line 05" is the key to the problem. The image size of faceCard will change exactly as expected, getting larger or smaller gradually according to pinching. However, without "line 05", the image size change drastically, just something like "exponential" effect. How to interpret it?

My analysis of what's happening:


- in change, the transform (scale, pan…) is applied on the modified image


so without line 05:

- on .changed notification,

- you change scale (let's say 1.1)

- that enlarges the image

- then you keep this value as a starting point for scale

- on next call, you start scale from 1.1, and grow it to 1.15

- you apply this to the transformed image: so change from original is 1.1. * 1.15 = 1.265

Hence the exponential effect


with line 05:

- on .changed notification,

- you change scale (let's say 1.1)

- that enlarges the image

- then you reset this value as a starting point for scale

- on next call, you start scale from 1.0, and grow it to 1.05

- you apply this to the transformed image: so change from original is 1.1. * 1.05 = 1.155

Hence the additive effect

A further test to confirm what's happening:

- change line 05 with:

              recognizer.scale = 0.9


- you should see the image reduce even when you try to enlarge

Did you make the test ?

thanks a lot

how to interpret "scale"
 
 
Q