2010-09-20 7 views
2

J'ai une question sur la façon dont je devrais aborder la rotation d'un UIView avec des événements tactiles.Faire pivoter UIView en utilisant UITouch

Tout simplement, je veux faire pivoter un UIView autour d'un point d'ancrage, en fonction de l'endroit où je touche et éteint dans cette vue. Imaginez un bouton de volume sur une chaîne hi-fi, que vous tournez avec un doigt.

J'ai construit une méthode de rotation en utilisant CAKeyframeAnimation qui effectue un transform.rotation.z, mais ce dont je ne suis pas sûr, c'est si (a) je devrais utiliser cela en conjonction avec les méthodes tactiles, et (b) comment je peut obtenir la coordonnée/angle du toucher par rapport à l'UIView sous-jacent, et ensuite le tourner pour pointer sur le toucher.

J'espère que j'ai du sens ... Quelqu'un peut-il faire des suggestions?

Répondre

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

Qu'est-ce que duagramImageView? Le code fonctionne pour moi mais quand je commence à bouger la vue tourner de 180 degrés et après je peux l'utiliser comme un cadran. Toute aide? –

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

}

Mettez dans votre UIView sous-classe et voilà!

Questions connexes