2011-02-03 10 views
1

ImageView上に表示するオーバーレイビュー(自己描画シェイプあり)があります。ビューを移動可能、サイズ変更可能、回転可能にしたい。ユーザーは、オーバーレイを中央からドラッグして移動させたり、2つのサイド(右または下)のいずれかからドラッグしてサイズを変更することができます。私がまだできなかったことは、ユーザが左上端を動かすことによって回転させることです。iphoneでオーバーレイ表示を回転する

myView.transform = CGAffineTransformMakeRotation(angle * M_PI/180); 

しかし、どのように私は角度を計算することができユーザーがタッチに基づいて?何か案は?

答えて

2

最も簡単な方法は、回転値をプロパティとして与えるUIRotationGestureRecognizerを使用することです。

あなたは(未テスト)は、このような何かをしようと、ジェスチャー認識を使用することができない場合は、次の

// Assuming centerPoint is the center point of the object you want to rotate (the rotation axis), 
// currentTouchLocation and initialTouchLocation are the coordinates of the points between 
// which you want to calculate the angle. 
CGPoint centerPoint = ... 
CGPoint currentTouchLocation = ... 
CGPoint initialTouchLocation = ... 

// Convert to polar coordinates with the centerPoint being (0,0) 
CGPoint currentTouchLocationNormalized = CGPointMake(currentTouchLocation.x - centerPoint.x, currentTouchLocation.y - centerPoint.y); 
CGPoint initialTouchLocationNormalized = CGPointMake(initialTouchLocation.x - centerPoint.x, initialTouchLocation.y - centerPoint.y); 

CGFloat angleBetweenInitialTouchAndCenter = atan2(initialTouchLocationNormalized.y, initialTouchLocationNormalized.x); 
CGFloat angleBetweenCurrentTouchAndCenter = atan2(currentTouchLocationNormalized.y, currentTouchLocationNormalized.x); 

CGFloat rotationAngle = angleBetweenCurrentTouchAndCenter - angleBetweenInitialTouchAndCenter; 

は、極座標の詳細とどのようにデカルトと極の間で変換する方法を学ぶためにウィキペディアを参照するか、Google検索を行います座標系。

+0

答えをありがとう。残念ながら、ジェスチャーは私が好むユーザーインタラクション体験を私に提供しません。例えば、ジェスチャーでは、幅を広げてビューの高さを固定することはできません。さらに、ジェスチャを使用すると、1本の指でオーバーレイを移動することができなくなります。 – adranale

+0

それは何百万回も感謝しました! – adranale