I am doing an app which has a feature to rotate and re size a view. i have implemented this feature but i do face an issue.
My problem
The View wil be resized when dragging its four corners, after resizing it i can rotate the view in both directions.
Once the rotation is done, if i try again to resize the view by dragging its corner, the view's size gone to unpredictable value and its moving all around the screen.
I googled lot finally i got the following solution
The frame property is undefined when transform != CGAffineTransformIdentity, as per the docs on UIView
I saw one app which has implemented the feature exactly what i wish to implement.
How can i resize the UIView after rotation of UIView
My code for resize the view
Touches Began
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
NSLog(@"[touch view]:::%@",[touch view]);
touchStart = [[touches anyObject] locationInView:testVw];
isResizingLR = (testVw.bounds.size.width - touchStart.x < kResizeThumbSize && testVw.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (testVw.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && testVw.bounds.size.height -touchStart.y <kResizeThumbSize);
}
Touches Moved
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:testVw];
CGPoint previous=[[touches anyObject]previousLocationInView:testVw];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
NSLog(@"CVTM:%@",NSStringFromCGRect(testVw.frame));
if (isResizingLR) {
testVw.frame = CGRectMake(testVw.frame.origin.x, testVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
}
if (isResizingUL) {
testVw.frame = CGRectMake(testVw.frame.origin.x + deltaWidth, testVw.frame.origin.y + deltaHeight, testVw.frame.size.width - deltaWidth, testVw.frame.size.height - deltaHeight);
}
if (isResizingUR) {
testVw.frame = CGRectMake(testVw.frame.origin.x ,testVw.frame.origin.y + deltaHeight, testVw.frame.size.width + deltaWidth, testVw.frame.size.height - deltaHeight);
}
if (isResizingLL) {
testVw.frame = CGRectMake(testVw.frame.origin.x + deltaWidth ,testVw.frame.origin.y , testVw.frame.size.width - deltaWidth, testVw.frame.size.height + deltaHeight);
}
if (!isResizingUL && !isResizingLR && !isResizingUR && !isResizingLL) {
testVw.center = CGPointMake(testVw.center.x + touchPoint.x - touchStart.x,testVw.center.y + touchPoint.y - touchStart.y);
}
}
Since you don't use UIView animations you can save current view's transform, set view's transform to identity, resize the view and reapply saved transform:
(EDIT)About your resizing:
If you define your rectangle with 4 vectors (points) A,B,C,D where
(A+C)*.5 = (B+D)*.5 = rectangle_center
Then for moving point C to position C' would also move B and D to B' and D':After that:
(.0f, .0f, length(A-D), length(A-C))
(A+D)*.5f
atanf(height/width)
and few "if's" OR fill/create transform with base vectors that arenormalized(D-A)
andnormalized(B-A)
You can do that for any point in a rectangle.