iOS で OpenGLES 正投影で表示された画像をズームするピンチを実装しようとすると、問題が発生します。ズームがピンチ位置の下の中心に留まるように、投影行列をスケーリングおよび変換しようとしています。
私の update: メソッドの下で、マトリックスを構成します。
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
projectionMatrix = GLKMatrix4MakeOrtho(-0.5f, 0.5f, -0.5f / aspect, 0.5f / aspect, -1.0f, 1.0f);
projectionMatrix = GLKMatrix4Multiply(projectionMatrix, GLKMatrix4MakeScale(newScale, newScale, 1.0f)); // scale
projectionMatrix = GLKMatrix4Multiply(projectionMatrix, GLKMatrix4MakeTranslation((endPan1.x + endPan2.x), 0.0 - (endPan1.y + endPan2.y), 0.0f)); //pan
ピンチ ジェスチャ レコグナイザを使用して、newScale と endPan1 の値を取得しています。(endPan2 は 2 本指のパン レコグナイザーに由来します。):
- (void)pinchDetected:(UIGestureRecognizer *)sender;
{
if (sender.state == UIGestureRecognizerStateBegan) {
lastScale = newScale;
startPan1.x = endPan1.x;
startPan1.y = endPan1.y;
GLKMatrix4 pinchMatrix = projectionMatrix;
CGFloat aspectInv = self.view.bounds.size.height / self.view.bounds.size.width;
// Get the center offset of the projection matrix:
GLKVector2 middleVect = GLKVector2Make(pinchMatrix.m30, pinchMatrix.m31);
CGPoint pointPinch = [sender locationInView:self.view];
GLKVector2 touchVectPinch = GLKVector2Make((2 * pointPinch.x / self.view.bounds.size.width - 1),
(2 * (self.view.bounds.size.height-pointPinch.y) / self.view.bounds.size.height - 1));
touchToMiddle = GLKVector2Make((middleVect.x - touchVectPinch.x) * (1.0 / pinchMatrix.m00), (middleVect.y - touchVectPinch.y) * ((1.0 / pinchMatrix.m00) * aspectInv) );
}
newScale = [(UIPinchGestureRecognizer*)sender scale] - (1.0 - lastScale);
if (newScale < 0.25) {
newScale = 0.25;
}
if (newScale > 4.0) {
newScale = 4.0;
}
// Pan the image to center the scale under the touch point:???
endPan1.x = (((newScale - 1.0) * 0.5) * (touchToMiddle.x )) - startPan1.x ;
endPan1.y = 0.0 - ((((newScale - 1.0) * 0.5) * (touchToMiddle.y )) + startPan1.y);
}
その結果、ズーム中に画像がピンチ位置の下で完全に中央にとどまりません。それはちょっと半分うまくいきます...私は近くにいるに違いないと思いますか???