如何通过使用Swift移动其中一个来相对移动对象的数量?

时间:2015-03-04 10:47:46

标签: ios swift uipangesturerecognizer

我有一个问题。

我有4个Image View对象是我的故事板。那些代表"角落"。我希望有这个选项,当你移动一个角落,其他三个相对移动。

喜欢animated gif here

我现在所做的是在故事板中将Pan Gesture Recognizer应用于图像视图。然后我将代码添加到ViewController:

 @IBAction func handlePan(recognizer:UIPanGestureRecognizer) {

    let translation = recognizer.translationInView(self.view)
    recognizer.view!.center = CGPoint(x:recognizer.view!.center.x + translation.x,
        y:recognizer.view!.center.y + translation.y)
    recognizer.setTranslation(CGPointZero, inView: self.view)

}

但我不知道接下来该做什么。我需要识别哪个角被轻敲和移动,所以我可以在其他角落应用特殊关系移动功能。

1 个答案:

答案 0 :(得分:0)

Heyo,

一种技术是找到表示平移手势的向量,然后使用该向量更新其余的图像视图。例如,UIPanGestureRecognizers扩展自UIGestureRecognizer,它具有以下方法:

func locationOfTouch(_ touchIndex: Int, inView view: UIView?) -> CGPoint

伪代码有点像这样:

  1. 获取1张imageView的最后一个已知位置作为CGPoint
  2. 检测平移手势
  3. 以CGPoint
  4. 的形式检索触摸位置

    现在您有两个CGPoints,1表示起始位置,1表示结束位置,您可以减去这两个点以形成具有长度和方向的矢量。使用此信息,您可以提出一个公式来替换其他3个imageViews。