2011-02-03 3 views
1

ImageView에서 표시하는 오버레이보기 (자체 모양이 있음)가 있습니다. 나는보기가 이동 가능하고, 크기를 조정할 수 있고, 회전 할 수 있기를 원한다. 사용자가 가운데에서 드래그하여 오버레이를 이동하거나 두면 중 하나 (오른쪽 또는 하단)에서 드래그하여 크기를 조절할 수 있습니다. 내가 할 수 없었던 것은 사용자가 왼쪽 상단 모서리를 움직여 회전시킬 수있게하는 것입니다.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

답변 해 주셔서 감사합니다. 불행히도 제스처는 내가 선호하는 사용자 상호 작용 경험을 제공하지 않습니다. 예를 들어 몸짓으로 너비를 늘리고보기 높이를 고정하는 것은 불가능합니다. 또한 제스처를 사용하면 하나의 손가락으로 오버레이를 움직일 수 없습니다. – adranale

+0

그것은 백만 번 감사했습니다! – adranale