【问题标题】:How to resize the UIView when CGAffineTransformIdentityCGAffineTransformIdentity时如何调整UIView的大小
【发布时间】:2012-10-01 09:36:10
【问题描述】:

我正在做一个具有旋转和调整视图大小功能的应用程序。我已经实现了这个功能,但我确实面临一个问题。

我的问题

拖动四个角时会调整视图的大小,调整大小后我可以双向旋转视图。

旋转完成后,如果我再次尝试通过拖动视图的角来调整视图的大小,视图的大小会变为不可预测的值,并且会在屏幕上移动。

我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);
 }

 }

【问题讨论】:

  • 我有完全相同的问题,我想知道是否有任何解决方案。如果有,请发帖。

标签: ios uiview resize rotation cgaffinetransform


【解决方案1】:

由于您不使用 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':

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) 的基本向量的变换来获得旋转以创建视图的变换

您可以对矩形中的任何点执行此操作。

【讨论】:

  • 实际上,您的代码中还有很多其他问题。我认为如果你尝试只有 4 个可移动点然后根据这 4 个点设置视图会更好。你需要更多的代码来进行这样的调整大小和移动。
猜你喜欢
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 2013-07-16
相关资源
最近更新 更多