iPhone多点触摸移动、缩放和旋转,如何防止缩放?

iPhone多点触摸移动、缩放和旋转,如何防止缩放?,iphone,rotation,multi-touch,Iphone,Rotation,Multi Touch,我有跟踪多点触摸位置的现有代码,然后适当地移动、旋转和缩放项目(在本例中为图像) 代码运行得很好,本身也很完美,但是对于这个特殊的任务,我只需要移动和旋转。我花了很多时间试着弄清楚这套程序是怎么回事,但数学不是我的强项,所以我想看看是否有人能帮忙 - (CGAffineTransform)incrementalTransformWithTouches:(NSSet *)touches { NSArray *sortedTouches = [[touches allObjects] sor

我有跟踪多点触摸位置的现有代码,然后适当地移动、旋转和缩放项目(在本例中为图像)

代码运行得很好,本身也很完美,但是对于这个特殊的任务,我只需要移动和旋转。我花了很多时间试着弄清楚这套程序是怎么回事,但数学不是我的强项,所以我想看看是否有人能帮忙

- (CGAffineTransform)incrementalTransformWithTouches:(NSSet *)touches
{
    NSArray *sortedTouches = [[touches allObjects] sortedArrayUsingSelector:@selector(compareAddress:)];
    NSInteger numTouches = [sortedTouches count];

 // No touches
 if (numTouches == 0) {
        return CGAffineTransformIdentity;
    }

 // Single touch
 if (numTouches == 1) {
        UITouch *touch = [sortedTouches objectAtIndex:0];
        CGPoint beginPoint = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch);
        CGPoint currentPoint = [touch locationInView:self.superview];
  return CGAffineTransformMakeTranslation(currentPoint.x - beginPoint.x, currentPoint.y - beginPoint.y);
 }

 // If two or more touches, go with the first two (sorted by address)
 UITouch *touch1 = [sortedTouches objectAtIndex:0];
 UITouch *touch2 = [sortedTouches objectAtIndex:1];

    CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);
    CGPoint currentPoint1 = [touch1 locationInView:self.superview];
    CGPoint beginPoint2 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch2);
    CGPoint currentPoint2 = [touch2 locationInView:self.superview];

 double layerX = self.center.x;
 double layerY = self.center.y;

 double x1 = beginPoint1.x - layerX;
 double y1 = beginPoint1.y - layerY;
 double x2 = beginPoint2.x - layerX;
 double y2 = beginPoint2.y - layerY;
 double x3 = currentPoint1.x - layerX;
 double y3 = currentPoint1.y - layerY;
 double x4 = currentPoint2.x - layerX;
 double y4 = currentPoint2.y - layerY;

 // Solve the system:
 //   [a b t1, -b a t2, 0 0 1] * [x1, y1, 1] = [x3, y3, 1]
 //   [a b t1, -b a t2, 0 0 1] * [x2, y2, 1] = [x4, y4, 1]

 double D = (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
 if (D < 0.1) {
        return CGAffineTransformMakeTranslation(x3-x1, y3-y1);
    }

 double a = (y1-y2)*(y3-y4) + (x1-x2)*(x3-x4);
 double b = (y1-y2)*(x3-x4) - (x1-x2)*(y3-y4);
 double tx = (y1*x2 - x1*y2)*(y4-y3) - (x1*x2 + y1*y2)*(x3+x4) + x3*(y2*y2 + x2*x2) + x4*(y1*y1 + x1*x1);
 double ty = (x1*x2 + y1*y2)*(-y4-y3) + (y1*x2 - x1*y2)*(x3-x4) + y3*(y2*y2 + x2*x2) + y4*(y1*y1 + x1*x1);

 return CGAffineTransformMake(a/D, -b/D, b/D, a/D, tx/D, ty/D);
}
我曾试图阅读《黑客帝国》的工作方式,但无法完全理解。更可能的问题是计算,正如我提到的,这不是我的强项

从这个例程中我需要的是一个变换,它执行我的移动和旋转,但忽略比例-因此忽略两个手指接触点之间的距离,并且不影响比例

我在互联网上看过其他处理多点触摸旋转的程序,但我尝试过的所有程序都存在一些问题,比如在抬起手指时跳跃等,而上面的代码是移动、缩放和旋转动作的关键代码


感谢您的帮助

这样做:

CGAffineTransform transform = self.view.transform;
float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
self.view.transform = CGAffineTransformScale(transform, 1/scale, 1/scale);
在触摸结束时,Moved:withEvent:和updateOriginalTransformForTouches方法。基本上,计算当前比例值,然后将变换矩阵与反向比例值相乘