我正在做一个应用程序,它具有旋转和重新调整视图大小的功能。我已实现此功能,但我确实遇到了问题。
我的问题
在拖动四个角时,视图会被调整大小,调整大小后我可以在两个方向上旋转视图。
旋转完成后,如果我再次尝试通过拖动角落来调整视图大小,则视图的大小会变为不可预测的值并且会在屏幕上移动。
我google了很多,最后我得到了以下解决方案
The frame property is undefined when transform != CGAffineTransformIdentity, as per the docs on UIView
我看到一个应用程序已经实现了我希望实现的功能。
如何在旋转UIView后重新调整UIView
我调整视图大小的代码
触动开始
- (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);
}
触动移动
- (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);
}
}
答案 0 :(得分:0)
由于您不使用UIView动画,您可以保存当前视图的变换,将视图的变换设置为标识,调整视图大小并重新应用保存的变换:
UIView *v;
CGAffineTransform t = v.transform;
v.transform = CGAffineTransformIdentity;
CGFloat scale = 2.0f;
v.frame = CGRectMake(v.frame.origin.x, v.frame.origin.y, v.frame.size.width*scale , v.frame.size.height*scale);
v.transform = t;
(编辑)关于你的大小调整:
如果你定义你的矩形有4个向量(点)A,B,C,D其中(A+C)*.5 = (B+D)*.5 = rectangle_center
那么将C点移动到位置C'也会将B和D移动到B'和D':< / p>
A' = A
C' = C' //touch input
B' = A + (normalized(B-A) * dotProduct((C'-A), normalized(B-A)))
D' = A + (normalized(D-A) * dotProduct((C'-A), normalized(D-A)))
之后:
(.0f, .0f, length(A-D), length(A-C))
(A+D)*.5f
atanf(height/width)
获取旋转以创建视图的变换,使用normalized(D-A)
和normalized(B-A)
你可以为矩形中的任何一点做到这一点。