I have a UIView selectFrame with four corner handles as subviews. Each corner handle has a UIPanGestureRecognizer which calls this method:
This is my code (where self is the selectFrame UIView):
-(void)handleDrag:(UIPanGestureRecognizer *)gesture
{
UIView *view = gesture.view;
CGPoint translation = [gesture translationInView:view.superview];
CGRect frame = self.bounds;
switch (view.tag) {
case (DRAG_HANDLE_TAG): //upper left
frame.size.width -= translation.x;
frame.size.height -= translation.y;
break;
case (DRAG_HANDLE_TAG+1): //upper right
frame.size.width += translation.x;
frame.size.height -= translation.y;
break;
case (DRAG_HANDLE_TAG+2): //bottom left
frame.size.width -= translation.x;
frame.size.height += translation.y;
break;
case (DRAG_HANDLE_TAG+3): //bottom right
frame.size.width += translation.x;
frame.size.height += translation.y;
break;
}
self.bounds = CGRectIntegral(frame);
CGAffineTransform transform = self.transform;
transform = CGAffineTransformTranslate(transform, translation.x/2, translation.y/2);
self.transform = transform;
[gesture setTranslation:CGPointZero inView:view.superview];
}
This code works really well without a rotation, but with the UIView rotated, it drifts.
However, if I do the whole calculation at the end of the gesture, by putting
if (gesture.state != UIGestureRecognizerStateEnded)
return;
at the top of the method, it works well even with the rotation, so it seems that the code works, but could it be rounding errors?
This question seems to have been asked in many different guises, but I can't find, after many hours of looking, one exactly answered. There are a number of StickerView examples available that resize from the center. This question Resize UIView While It Is Rotated with Transform receives a very full answer, but it doesn't really answer the question.
Edit: There are a lot of responses suggesting trigonometry, and I have attempted this, but it seems to me that it should be possible simply using CGAffineTransforms. I have experimented with this, but with no good result.