CGAffineTransformIdentity时如何调整UIView的大小 [英] How to resize the UIView when CGAffineTransformIdentity
问题描述
我正在做一个具有旋转和调整视图大小功能的应用程序.我已经实现了此功能,但确实遇到了问题.
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.
我的问题
拖动视图的四个角时,将调整视图的大小,调整大小后,我可以在两个方向上旋转视图.
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.
旋转UIView后如何调整UIView的大小
How can i resize the UIView after rotation of 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);
}
}
推荐答案
由于您不使用UIView动画,因此可以保存当前视图的变换,将视图的变换设置为身份,调整视图的大小并重新应用保存的变换:
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:
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;
(编辑)关于您调整大小:
(EDIT)About your resizing:
如果您使用4个向量(点)A,B,C,D定义矩形,其中(A+C)*.5 = (B+D)*.5 = rectangle_center
然后将点C移动到位置C'也会将B和D移动到B'和D':
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':
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)
的基本矢量填充少数"if"或进行填充/创建变换
- transform your view to identity
- set frame to
(.0f, .0f, length(A-D), length(A-C))
- set center to
(A+D)*.5f
- get rotation to create view's transform through
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.
这篇关于CGAffineTransformIdentity时如何调整UIView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!