2014-05-01 105 views
2

我想在iOS應用程序中縮放一個四元組。它不需要基於四邊形的中心進行縮放,而是基於捏的質心進行縮放。如何準確放大捏手勢上的特定點(多捏)?

我能夠正確地做到這一點 - 但僅限於第一個捏手勢。在後來的捏手勢,它的工作原理,但它漂移一點點,似乎不太準確。我無法弄清楚該做什麼。

圍繞這個問題有幾個SO問題,並且我經歷了大多數(如果不是全部的話)。他們沒有一個能準確解決我的問題。

另請注意,我正在縮放和翻譯一個四邊形(渲染爲GLKView),而不是視圖本身。我見過的大多數解決方案都是直接轉換視圖。

下面是縮放手勢和處理代碼: 首先在viewDidLoad中:

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] 
              initWithTarget:self action:@selector(respondToPinchGesture:)]; 
pinchRecognizer.cancelsTouchesInView = YES; 
pinchRecognizer.delaysTouchesEnded = NO; 
[glView addGestureRecognizer:pinchRecognizer]; 

凡glView是GLKView對象。 和處理程序:

- (IBAction)respondToPinchGesture:(UIPinchGestureRecognizer *)recognizer{ 

    if (recognizer.state == UIGestureRecognizerStateEnded || [recognizer numberOfTouches] < 2) return; 

    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     point = [recognizer locationInView:glView]; 
     point.x *= glView.contentScaleFactor; 
     point.y *= glView.contentScaleFactor; 
     point.y = height - point.y; 

     anchor = GLKVector3Make(point.x, point.y, 0); 
     lastScale = 1.0; 
    } 

    if (fabs(recognizer.scale - lastScale) > 0.01){ 

     GLfloat scale = 1.0 - (lastScale - recognizer.scale); 
     lastScale = recognizer.scale; 

     new_anchor_point = anchor; 
     new_anchor_point = GLKVector3MultiplyScalar(new_anchor_point, scale); 
     GLKVector3 translate = GLKVector3Subtract(anchor, new_anchor_point); 

     path.transform = GLKMatrix4TranslateWithVector3(path.transform, translate); 
     path.transform = GLKMatrix4Scale(path.transform, scale, scale, 0); 

     cumulative_translate = GLKVector3Add(cumulative_translate, translate); 
    } 

} 

任何指針讚賞。我有2天的時間,甚至一個模糊的建議可能會有所幫助。

回答

-1

我設法用這個來解決:

-(GLKVector3)get_touch_point_on_view:(UIGestureRecognizer *)recognizer{ 
    CGRect bounds = [glView bounds]; 
    CGPoint point = [recognizer locationInView:glView]; 
    point.y = bounds.size.height - point.y; 
    return GLKVector3Make((point.x * glView.contentScaleFactor - total_translation.x)/total_scale, 
            (point.y * glView.contentScaleFactor - total_translation.y)/total_scale, 0); 
} 
- (void)respondToPinchGesture:(UIPinchGestureRecognizer *)recognizer{ 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     lastScale = 1.0; 
    } 

    [self get_touch_point_on_view:recognizer]; 

    if (fabs(recognizer.scale - lastScale) > 0.01){ 

     GLfloat scale = 1.0 - (lastScale - recognizer.scale); 
     lastScale = recognizer.scale; 
     total_scale *= scale; 

     path.transform = GLKMatrix4TranslateWithVector3(path.transform, centroid); 
     path.transform = GLKMatrix4Scale(path.transform, scale, scale, 0); 
     path.transform = GLKMatrix4TranslateWithVector3(path.transform, GLKVector3Negate(centroid)); 
     total_translation = [self get_total_translation]; 

    } 
} 
0

你必須

  • 記得在RecognizerStateBegan先前的變換矩陣,
  • 構建新的變換矩陣爲雙指縮放假設之前尚未轉化的觀點或對象。
  • 然後,將兩個矩陣連接在一起。這將是您轉換對象或視圖的最終矩陣。
+0

這是顯而易見的。我很清楚可以進行縮放。問題是隨後的夾點漂移。你真的應該詳細閱讀這個問題。 –