UIPinchGestureRecognizer定位两个手指之间的压缩视图 [英] UIPinchGestureRecognizer position the pinched view between the two fingers
问题描述
我成功实现了捏缩放视图。但是,这种观点并没有将自己定位在我希望的位置。对于带有iPad的stackoverflowers,我希望我的视图能够像iPad Photos.app一样居中:当您捏合和放大相册时,照片会出现在正在扩展的视图中。该视图大致位于第一个手指的右上角和另一个手指的左下角。我用pan识别器混合它,但这样用户总是需要捏,然后平移以进行调整。
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 ^^)
推荐答案
你可以得到 CGPoint 通过方法
handlingPinchGesture
中的以下代码,两个手指之间的中点code>。
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
方法如下。
/*
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屋!