I have drawn a UIBezierPath
inside a UIView
. With a pinchGesture
I try to scaled it up and down.
The scaling works, but my problem is that the UIBezierPath
moves down/upward (if it increases in size it moves downward if the size decreases it moves upward).
How is it possible to set the anchor point for the scaling so the UIBezierPath
center stays in the same place?
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
CGAffineTransform transform = CGAffineTransformScale(recognizer.view.transform,
recognizer.scale,
recognizer.scale);
[self.selectedObject applyAffineTransform:transform];
recognizer.scale = 1;
}
(selectedObject is an object which just applies the affineTransform
to its UIBezierPath
)
I tried to apply a translation in addition to the scaling to move it back where it was before, but I don't know how to find the values of the translation so the path stays in the same place.
Ok after many tries I managed to solve it, after the scaling i need to add a translation, for the distance of the translation I needed to compare the bounds of my uibezierpath and substract the change in origine and half the increase in width and height here is the same translated in code:
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
CGRect originalBounds = self.selectedObject.bounds;
CGAffineTransform scale = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
[self.selectedObject applyAffineTransform:scale];
CGAffineTransform translate = CGAffineTransformMakeTranslation(-(self.selectedObject.bounds.origin.x - originalBounds.origin.x) - (self.selectedObject.bounds.size.width - originalBounds.size.width) * 0.5, -(self.selectedObject.bounds.origin.y - originalBounds.origin.y) - (self.selectedObject.bounds.size.height - originalBounds.size.height) * 0.5);
[self.selectedObject applyAffineTransform:translate];
recognizer.scale = 1;
}
I hope this will help someone else as well.