When I programmatically set the scale of a UIScrollView to a certain ratio of the screen in scrollViewDidEndZooming,then scrollViewDidZoom is invoked once more by the framework automatically and so is scrollViewDidEndZooming !
To avoid an infinite loop I compare the scale property once more against my required zoom-levels (1 // 1.2857 // 1.8 // 3 and 9) to let the function pass through in case of an unchanged scale-value. When zooming in one direction the values as I set them in the first pass are reported identical when scrollViewDidEndZooming gets invoked for a second time by the framework. When zooming in the other direction the two CGFloats are 'off' due to rounding errors (1 // 1.28569996 // 1.79999995 // 3 and 9).
My problem now is:
No matter, how I round(), assign or cast my off-values, when finally assigning them to a CGFloat the debugger reports back the same old off-value and my comparison function fails and causes undesired jumps between my predfined zoom-levels.
I could of course set the threshhold values of my function to the off-values reported by the debugger - and actually it works this way now - but I am not sure if this is very future-proof and also as a consequence eventually causes a 1-pixel-off glitch and puts my views on the wrong spot on screen.
So I'd prefer an accurate procedure. But I can't figure out how to round my 1.79999995 to 1.8 in an other way...
Any ideas?
It may be irritating, but it isn't odd. 🙂
If you never calculated a floating point value, but only ever stored constants and compared between them, nothing odd would ever happen. The oddness results from comparing a constant and a calculated value, or two calculated values.
Even if you are not doing the calcuations yourself, you may be using calculated values from the Cocoa frameworks. For example, if you set a CGFloat scale factor on a view, then retrieve the scale factor property, you don't know whether the scale factor given back to you was (say) recalculated from a view frame rect, or was stored internally in a different precision that might slightly change the value.
Inspecting the returned value in the debugger won't help, because the original and re-calculated values may have the same decimal representation, but be different binary values.