触摸位置UIPinchGestureRecognizer

时间:2015-01-07 04:10:28

标签: ios swift sprite-kit

我正在尝试使用UIPinchGesture识别器调整图像大小,为了实现这一点,我需要找到捏合所关注的中心点的位置。

最初我考虑过根据两个触点创建中心点的中点计算。问题是由于某种原因,使用触摸索引的返回点不是在屏幕上应用的触摸位置。

例如,当我尝试使用大约在(333,187)处的触摸进行缩放而在(1000,563)处进行另一次触摸时,触摸的返回位置是(496,279)和(170,95)。 UITouch 1UITouch 2指数到底是什么?如何找到中心点值?

func handlePinchGesture(gesture: UIPinchGestureRecognizer){

    // Finds the midpoint location of the pinch gesture
    var touch1 = gesture.locationOfTouch(0, inView: self.view)
    var touch2 = gesture.locationOfTouch(1, inView: self.view)
    var midPointX = (touch1.x + touch2.x)/2
    var midPointY = (touch1.y + touch2.y)/2
    var touchedPoint = CGPointMake(midPointX, midPointY)
}

2 个答案:

答案 0 :(得分:0)

  

如何找到中心点值

您无需找到它。手势识别器会将它呈现给您。这是手势识别器的locationInView:

答案 1 :(得分:0)

CGPoint centerPoint = [recognizer locationInView: self.view];

Apple says

  

返回值是UIKit框架计算的手势的通用单点位置。通常是手势中涉及的触摸的质心

这将为您提供中心。希望这有助于.. :))