2012-07-06 88 views
0

我實現了一個UIPanGestureRecognizer,因爲我希望用一根手指沿着它的軸旋轉一個UIView。 uiview中的按鈕開始UIView旋轉的手勢。問題是隻有當按鈕位於第一象限左上方時才能正確旋轉。任何其他象限和它不正常旋轉。有人能告訴我我的數學有什麼問題嗎?順便說一下,ang使用超視圖的座標來計算角度,因爲用戶的手指可能在旋轉視圖邊界之外,但這可能不是必需的。用一根手指旋轉UIView iPhone,iPad

謝謝

- (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; 
} 

回答

0

我做了一些記錄,似乎我的UIView中心觸拖事件過程中發生變化。因此,我用touches開始的方法存儲了uiview的中心,並用它來代替。

- (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; 
    }