2011-03-19 69 views
2

我正在使用CGAffineTransformMake來轉換​​視圖。它旋轉,縮放和翻譯。這工作得很好。但我無法想出一種將比例限制在最大值的方法。限制CGAffineTransform Scale

如果超過規模,我仍然需要應用當前的旋轉和平移。

任何建議,非常感謝!

來源:

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

回答

35

做這樣的事情:

CGAffineTransform transform = self.view.transform; 
float scale = sqrt(transform.a*transform.a + transform.c*transform.c); 
if (scale > SCALE_MAX) 
    self.view.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale); 
else if (scale < SCALE_MIN) 
    self.view.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale); 

在你touchesMoved結束:withEvent:方法和updateOriginalTransformForTouches方法。基本上,您檢查當前比例是否超過某個SCALE_MAX值,然後將您的變換矩陣與反轉比例值相乘。

+0

完美!非常感謝!因爲我沒有足夠的聲望,所以我不能投票回答你的答案。有人可以爲我投票嗎?如果可以的話,我會把它投票10次!再次感謝。 – 2011-04-22 04:34:14

+0

順便說一句:我有一個線性代數文本,我試圖通過學習這種東西。 – 2011-04-22 04:34:53

+0

這是完美的,謝謝! – 2011-08-31 08:12:42

1

如果有人需要@Enzo Tran的斯威夫特答案與UIPanGestureRecognizer:

func handlePinch(recognizer : UIPinchGestureRecognizer) { 
    if let view = recognizer.view { 
     view.transform = CGAffineTransformScale(view.transform, 
                recognizer.scale, recognizer.scale) 
     let transform = view.transform 
     let maxScale: CGFloat = 1.7 //Anything 
     let minScale: CGFloat = 0.5 //Anything 
     let scale = sqrt(transform.a * transform.a + transform.c * transform.c) 
     if scale > maxScale { 
      view.transform = CGAffineTransformScale(transform, maxScale/scale, maxScale/scale) 
     } 
     else if scale < minScale { 
      view.transform = CGAffineTransformScale(transform, minScale/scale, minScale/scale) 
     } 

     recognizer.scale = 1 
    } 
}