I'm using a UIPinchGestureRecognizer to resize the frames of views in my app. I'd like to resize height and width independently (the views are rectangles on a snap-to grid), and so I need to determine whether the pinch is intended to modify the height or the width.
Initially I tried using the slope between the two points, as many SO posts suggest. Something like this:
enum ScalingType {
case widthOnly, heightOnly, widthAndHeight
}
private func determineScaling(loc1: CGPoint, loc2: CGPoint) -> ScalingType {
let dx = abs(loc1.x - loc2.x)
let dy = abs(loc1.y - loc2.y)
if dx > dy {
return .widthOnly
} else {
return .heightOnly
}
}
While this works for most shapes, it does not work when the shape becomes very narrow in either dimension. For example, if I want to resize the height of a rectangle with short height and long width, the fingers will move vertically, but the fingers will probably have more horizontal distance than vertical.
Is there a best way to get (for example) the translation of points in a pinch gesture or some way to determine the direction the fingers have moved?
Thanks