iOS:单指旋转和图像转换

时间:2011-11-10 18:50:11

标签: iphone ipad uitouch

目前我跟着这个Link对图像执行单指旋转并且工作正常。但是我想知道在执行旋转之后,如果我想从一个位置移动图像,如何执行翻译操作屏幕到任何其他位置。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // We can look at any touch object since we know we 
   // have only 1. If there were more than 1 then 
   // touchesBegan:withEvent: would have failed the recognizer.
   UITouch *touch = [touches anyObject];

   // A tap can have slight movement, but we're not interested
   // in a tap. We want more movement. So if a tap is detected
   // fail the recognizer. 
   if ([self state] == UIGestureRecognizerStatePossible) {
      [self setState:UIGestureRecognizerStateBegan];
   } else {
      [self setState:UIGestureRecognizerStateChanged];
   }

   // To rotate with one finger, we simulate a second finger.
   // The second figure is on the opposite side of the virtual
   // circle that represents the rotation gesture.

   UIView *view = [self view];
   CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
   CGPoint currentTouchPoint = [touch locationInView:view];
   CGPoint previousTouchPoint = [touch previousLocationInView:view];

   // use the movement of the touch to decide
   // how much to rotate the carousel
   CGPoint line2Start = currentTouchPoint;
   CGPoint line1Start = previousTouchPoint;
   CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y));
   CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y));

   //////
   // Calculate the angle in radians.
   // (From: http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html )
   CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;

   CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
   CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

    CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));

   CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs;
   //////

   [self setRotation:angleInRadians];
}

1 个答案:

答案 0 :(得分:1)

我不确定我的回答会对你有所帮助,但我担心你不能同时进行“一键式”轮换和“一键式”翻译。实际上,您的代码总是通过“模拟”触摸显示屏的第二根手指的存在来解释单次触摸旋转。

你可以做的是尝试和区分两个单指手势,以便将一个与旋转相关联,另一个与翻译相关联。例如,用于翻译的手势可以是“长触摸”(即,您触摸并保持触摸一段时间,然后移动对象)。

希望这有帮助,无论如何......