Question

I'm trying to set a max and a min limit to the zooming when I use the pinch to zoom gesture

CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
NSLog(@"Pinch scale: %f", recognizer.scale);
float scale = recognizer.scale;
float SCALE_MIN = 1.0f;
float SCALE_MAX = 3.0f;

if (SCALE_MIN < scale < SCALE_MAX) {
    self.view.transform = transform;
}

else {

}

The logic behind this, was that it would only zoom if the condition was satisfied

but this just zooms endlessly.

Was it helpful?

Solution

C is not Python. The < operator is binary and left-associative, so

SCALE_MIN < scale < SCALE_MAX

is parsed as

(SCALE_MIN < scale) < SCALE_MAX

The left hand side is either 1 or 0 (true or false), which is always smaller than SCALE_MAX (which is 3). So your condition is always true. (Did you not get a compiler warning regarding that?)

Hint: you need to use the logical AND (&&) operator to combine the two conditions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top