UIPinchGestureRecognizer定位两个手指之间的压缩视图

时间:2011-02-09 15:49:22

标签: ipad ios multi-touch

我成功实现了捏缩放视图。但是,这种观点并没有将自己定位在我希望的位置。对于带有iPad的stackoverflowers,我希望我的视图像iPad Photos.app一样居中:当你捏合和放大相册时,照片会出现在正在扩展的视图中。该视图大致以第一个手指的右上角和另一个手指的左下角手指为中心。我将其与平底锅识别器混合,但这样用户总是需要捏,然后平移以进行调整。

以下是图解说明,如果不清楚,我可以发布我的应用视频(不是秘密,我正在尝试重现iPad的Photos.app ......)

因此,对于手指的初始位置,开始缩放:

enter image description here

这是现在的实际“缩放”框架。方块较大,但位置低于手指

given the start position

这是我想要的:相同的大小,但不同的origin.x和y:

enter image description here

(抱歉我糟糕的Photoshop技能^^)

2 个答案:

答案 0 :(得分:42)

您可以通过CGPoint方法中的以下代码获取两根手指之间的handlingPinchGesture点。

CGPoint point = [sender locationInView:self];

我的整个handlePinchGesture方法如下。

/*
instance variables

CGFloat lastScale;
CGPoint lastPoint;
*/

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
    if ([sender numberOfTouches] < 2)
        return;

    if (sender.state == UIGestureRecognizerStateBegan) {
        lastScale = 1.0;
        lastPoint = [sender locationInView:self];
    }

    // Scale
    CGFloat scale = 1.0 - (lastScale - sender.scale);
    [self.layer setAffineTransform:
        CGAffineTransformScale([self.layer affineTransform], 
                               scale, 
                               scale)];
    lastScale = sender.scale;

    // Translate
    CGPoint point = [sender locationInView:self];
    [self.layer setAffineTransform:
        CGAffineTransformTranslate([self.layer affineTransform], 
                                   point.x - lastPoint.x, 
                                   point.y - lastPoint.y)];
    lastPoint = [sender locationInView:self];
}

答案 1 :(得分:11)

查看Touches sample project。具体来说,这些方法可以帮助您:

// scale and rotation transforms are applied relative to the layer's anchor point
// this method moves a gesture recognizer's view's anchor point between the user's fingers
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:piece];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;
    }
}

// scale the piece by the current scale
// reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current scale
- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
    [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }
}