0

J'ai implémenté un UIPanGestureRecognizer puisque je souhaite utiliser un doigt pour faire pivoter un UIView le long de son axe. Un bouton à l'intérieur de l'uiview commence le geste auquel le UIView tourne. Le problème est qu'il tourne seulement correctement si le bouton est dans le 1er quadrant, en haut à gauche. Tout autre quadrant et il tourne de façon erratique. Quelqu'un peut-il me dire ce qui ne va pas avec mes maths? D'ailleurs, ang calcule l'angle en utilisant les coordonnées de la vue d'ensemble puisque le doigt de l'utilisateur peut être en dehors des limites des vues rotatives, mais cela peut ne pas être nécessaire.Rotation UIView avec 1 doigt iPhone, iPad

merci

- (void)rotateItem:(UIPanGestureRecognizer *)recognizer 
{ 
    NSLog(@"Rotate Item"); 

    float ang = atan2([recognizer locationInView:self.superview].y - self.center.y, [recognizer locationInView:self.superview].x - self.center.x); 

    float angleDiff = deltaAngle - ang; 
    self.transform = CGAffineTransformRotate(startTransform, -angleDiff); 

    CGFloat radians = atan2f(self.transform.b, self.transform.a); 
    NSLog(@"rad is %f", radians); 
} 

#pragma mark - Touch Methods 

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)recognizer 
{ 

    if (recognizer == rotateGesture) { 
     NSLog(@"rotate gesture started"); 

     deltaAngle = atan2([recognizer locationInView:self].y-self.center.y, [recognizer locationInView:self].x-self.center.x); 

     startTransform = self.transform; 
    } 

    return YES; 
} 

Répondre

0

Je l'ai fait une exploitation forestière et il semble que le centre de ma UIView changeait lors de l'événement de glisser contact. C'est pourquoi j'ai stocké le centre de l'uiview avec les touches a commencé la méthode et l'ai utilisé à la place.

- (void)rotateItem:(UIPanGestureRecognizer *)recognizer 
    { 
     NSLog(@"Rotate Item"); 

     CGPoint superPoint = [self convertPoint:itemCenter toView:self.superview]; 

     float ang = atan2([recognizer locationInView:self.superview].y - superPoint.y, [recognizer locationInView:self.superview].x - superPoint.x); 

     float angleDiff = deltaAngle - ang; 
     self.transform = CGAffineTransformRotate(startTransform, -angleDiff); 

    } 

    #pragma mark - Touch Methods 

    - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)recognizer 
    { 

     if (recognizer == rotateGesture) { 
      NSLog(@"rotate gesture started"); 

      deltaAngle = atan2([recognizer locationInView:self.superview].y-self.center.y, [recognizer locationInView:self.superview].x-self.center.x); 

      startTransform = self.transform; 
     } 

     return YES; 
    }