我想在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天的时间,甚至一个模糊的建议可能会有所帮助。
这是显而易见的。我很清楚可以进行缩放。问题是随后的夹点漂移。你真的应该详细阅读这个问题。 –