0

ビューを回転およびサイズ変更する機能を持つアプリを実行しています。この機能を実装しましたが、問題に直面しています。

私の問題

ビューの四隅をドラッグすると、ビューのサイズが変更されます。サイズを変更した後、ビューを両方向に回転させることができます。

回転が完了したら、コーナーをドラッグしてビューのサイズを変更しようとすると、ビューのサイズが予測できない値になり、画面全体に移動します。

私は多くのことをグーグルで検索し、最終的に次の解決策を得ました

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

 }
4

1 に答える 1

0

UIView アニメーションを使用しないため、現在のビューの変換を保存し、ビューの変換を ID に設定し、ビューのサイズを変更して、保存した変換を再適用できます。

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)、いくつかの「if」またはベースベクトルで塗りつぶし/変換を作成しnormalized(D-A)normalized(B-A)

長方形内の任意の点に対してこれを行うことができます。

于 2012-10-01T11:49:15.483 に答える