使用PinchGesture;如何放大用户手指实际“捏"的位置? [英] Using PinchGesture; how can I zoom in to where a user's fingers actually "pinch"?

查看:241
本文介绍了使用PinchGesture;如何放大用户手指实际“捏"的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序的UIImageView上实现了UIPinchGestureRecognizer,但是无论我在图像上的什么位置,它似乎都可以放大到同一位置.有谁知道我如何使它放大到用户实际捏"的地方?参见下面的代码.

I've implemented the UIPinchGestureRecognizer on a UIImageView in my app, however no matter where I pinch on the image, it seems to zoom into the same spot. Does anyone know how I can make it zoom in to where a user actually "pinches"? See code below.

ViewController.m

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

   recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
   recognizer.scale = 1; 

 }

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
    BOOL shouldReceiveTouch = YES;

    if (gestureRecognizer == tap) {
        shouldReceiveTouch = (touch.view == featureImage);
    }
    return shouldReceiveTouch;
}

推荐答案

缩放转换使原点(0,0)保持不变.因此,要围绕特定点缩放视图,必须首先将该点转换为原点,然后应用该比例,然后再平移.

A scale transform leaves the origin (0, 0) untouched. So to scale a view around a particular point, you must first translate that point to the origin, then apply the scale, then translate back.

- (IBAction)pinchGestureDidFire:(UIPinchGestureRecognizer *)pinch {

首先,我们将视图捏住.

First, we get the view being pinched.

    UIView *pinchView = pinch.view;

要计算收缩的中心,我们需要视图边界的中点,因此我们也要获得边界:

To compute the center of the pinch, we'll need the midpoint of the view's bounds, so we get the bounds too:

    CGRect bounds = pinchView.bounds;

中心基于捏触的质心,我们可以通过这种方式获得:

The center is based on the centroid of the pinch's touches, which we get this way:

    CGPoint pinchCenter = [pinch locationInView:pinchView];

但是我们实际上需要相对于视图中心的收缩偏移,因为默认情况下视图的变换是相对于视图中心的. (您可以通过更改视图的layer.anchorPoint来更改此设置.)

But we actually need the pinch offset relative to the center of the view, because the view's transform is relative to the center of the view by default. (You can change this by changing the view's layer.anchorPoint.)

    pinchCenter.x -= CGRectGetMidX(bounds);
    pinchCenter.y -= CGRectGetMidY(bounds);

现在,我们可以更新视图的变换.首先,我们得到它的当前变换:

Now we can update the view's transform. First we get its current transform:

    CGAffineTransform transform = pinchView.transform;

然后我们对其进行更新,以将捏合中心转换为原点:

Then we update it to translate the pinch center to the origin:

    transform = CGAffineTransformTranslate(transform, pinchCenter.x, pinchCenter.y);

现在我们可以应用比例尺了:

Now we can apply the scale:

    CGFloat scale = pinch.scale;
    transform = CGAffineTransformScale(transform, scale, scale);

然后我们将视图平移回去

Then we translate the view back:

    transform = CGAffineTransformTranslate(transform, -pinchCenter.x, -pinchCenter.y);

现在,我们可以使用修改后的变换更新视图:

Now we can update the view with the modified transform:

    pinchView.transform = transform;

最后,由于我们应用了当前比例尺,因此我们重置了手势识别器的比例尺:

Finally, we reset the gesture recognizer's scale, since we've applied the current scale:

    pinch.scale = 1.0;
}

演示:

请注意,在模拟器中,您可以按住捏手势的选项(alt).按住shift键(同时按住Option键)会将两个接触点同时移动.

Note that in the simulator, you can hold option (alt) for a pinch gesture. Holding shift (while holding option) moves the two touches together.

以下是用于复制/粘贴的所有代码:

Here's the code all together for copy/paste:

- (IBAction)pinchGestureDidFire:(UIPinchGestureRecognizer *)pinch {
    UIView *pinchView = pinch.view;
    CGRect bounds = pinchView.bounds;
    CGPoint pinchCenter = [pinch locationInView:pinchView];
    pinchCenter.x -= CGRectGetMidX(bounds);
    pinchCenter.y -= CGRectGetMidY(bounds);
    CGAffineTransform transform = pinchView.transform;
    transform = CGAffineTransformTranslate(transform, pinchCenter.x, pinchCenter.y);
    CGFloat scale = pinch.scale;
    transform = CGAffineTransformScale(transform, scale, scale);
    transform = CGAffineTransformTranslate(transform, -pinchCenter.x, -pinchCenter.y);
    pinchView.transform = transform;
    pinch.scale = 1.0;
}

这篇关于使用PinchGesture;如何放大用户手指实际“捏"的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆