2010-09-20 90 views
2

我對如何使用基於觸摸的事件旋轉UIView有一個疑問。使用UITouch旋轉UIView

很簡單,我想旋轉一個UIView圍繞一個定位點,這取決於我在該視圖中觸摸和關閉的位置。想象一下hifi上的音量旋鈕,您可以用一根手指旋轉。我已經構建了一個使用CAKeyframeAnimation執行transform.rotation.z的旋轉方法,但是我不確定的是如果(a)我應該將它與touch方法結合使用,以及(b)我如何可以獲得觸摸相對於底層UIView的座標/角度,並隨後將其指向觸摸。

我希望我有道理......任何人都可以提出任何建議嗎?

回答

1
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

CGPoint Location = [[touches anyObject] locationInView:self]; 

int x = ImageView.center.x; 
int y = ImageView.center.y; 

float dx = Location.x - x; 
float dy = Location.y - y; 

double a = atan2(-dx,dy); 
ImageView.layer.position = diagramImageView.center; 
ImageView.layer.transform = CATransform3DMakeRotation(a, 0, 0, 1); 
} 
+0

什麼是duagramImageView?代碼正在爲我工​​作,但當我開始移動視圖旋轉180度後,我可以像撥號一樣。 有什麼幫助嗎? – 2011-12-02 14:10:09

2
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

CGPoint newLocationPoint = [[touches anyObject] locationInView:self.superview]; 

int x = self.superview.center.x; 
int y = self.superview.center.y; 

float dx = newLocationPoint.x - x; 
float dy = newLocationPoint.y - y; 

double angle = atan2(-dx,dy); 

self.layer.position = self.superview.center; 
self.layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1); 

NSLog(@"%f,%f ", dx,dy); 

}

在你的UIView子類,瞧將這個!