UIPinchGestureRecognizer 将捏合视图定位在两个手指之间 [英] UIPinchGestureRecognizer position the pinched view between the two fingers

查看:29
本文介绍了UIPinchGestureRecognizer 将捏合视图定位在两个手指之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地实现了缩放视图.但是,该视图并未将自己定位在我希望的位置.对于 iPad 上的 stackoverflowers,我希望我的视图像 iPad Photos.app 一样居中:当你捏合和放大相册时,照片会在一个正在扩展的视图中显示出来.该视图大致居中,第一根手指的右上角和另一根手指的左下角手指.我将它与平移识别器混合使用,但这样用户总是必须捏合,然后平移才能调整.

I successfully implemented a pinch a zoom of a view. However, the view doesn't position itself where I wished it to be. For the stackoverflowers with an iPad, I would like my view to be centered like on the iPad Photos.app : when you pinch&zoom on an album, the photos present themselves in a view that is expanding. This view is approximately centered with the top right hand corner on the first finger and the bottom left hand finger on the other finger. I mixed it with a pan recognizer, but this way the user always has to pinch, and then pan to adjust.

这里有很形象的解释,如果不清楚,我可以发布我的应用程序的视频(不是秘密,我正在尝试复制 iPad 的 Photos.app...)

Here are so graphic explanation, I could post a video of my app if that's unclear (no secret, i'm trying to reproduce the Photos.app of the iPad...)

所以对于手指的初始位置,开始缩放:

So for an initial position of the fingers, begining zooming :

这是目前实际的缩放"框架.方块比较大,但是位置在手指下方

This is the actual "zoomed" frame for now. The square is bigger, but the position is below the fingers

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

Here is what I would like to have : same size, but different origin.x and y :

(抱歉我的photoshop技术不好^^)

(sorry about my poor photoshop skills ^^)

推荐答案

可以通过handlingPinchGesture方法中的以下代码获取两指间中点的CGPoint.

You can get the CGPoint of the midpoint between two fingers via the following code in the method handlingPinchGesture.

CGPoint point = [sender locationInView:self];

我的整个 handlePinchGesture 方法如下.

My whole handlePinchGesture method is below.

/*
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];
}

这篇关于UIPinchGestureRecognizer 将捏合视图定位在两个手指之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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