捏手势刻度重置为1?

时间:2015-03-26 13:41:43

标签: ios swift uigesturerecognizer scenekit uipinchgesturerecognizer

我正在看这个回购

https://github.com/mmohsin991/gestures/blob/master/gestures/ViewController.swift

和这个例子

http://www.raywenderlich.com/50398/opengl-es-transformations-gestures

我有以下代码

      @IBOutlet var pinchProperty: UIPinchGestureRecognizer!
      @IBAction func pinchAction(sender: UIPinchGestureRecognizer) {
        if(dbg){println("scale \(pinchProperty.scale)")}
        let pinch: CGFloat = pinchProperty.scale
        switch pinchProperty.state{
        case .Began:
          println("pinch started")
        case .Changed:
          geometryNodeMain.scale = SCNVector3(x:Float(pinch),y:Float(pinch),z:Float(pinch))
        case .Ended:
          println("pinch ended")
        case .Cancelled:
          break
        case .Failed:
          break
        case .Possible:
          break
        default:
          break
        }
      }

最初,捏合工作并放大和缩小,但随后刻度重置为1.转换openGL代码http://www.raywenderlich.com/50398/opengl-es-transformations-gestures很麻烦。

有人可以帮助转换此代码吗?

1 个答案:

答案 0 :(得分:4)

这很有效。

在某处初始化变量。

  var scaleStart:CGFloat = 1.0
  var scaleEnd:CGFloat = 1.0

然后就是这样的

 @IBOutlet var pinchProperty: UIPinchGestureRecognizer!
  @IBAction func pinchAction(sender: UIPinchGestureRecognizer) {

    if(dbg){println("scale \(pinchProperty.scale)")}

    let pinch: CGFloat = pinchProperty.scale

    switch pinchProperty.state{
    case .Began:
      scaleStart = scaleEnd*pinch
      if(dbg){println("pinch started \(scaleStart)")}
    case .Changed:
      scaleStart = scaleEnd*pinch
      geometryNodeMain.scale = SCNVector3(
        x:Float(scaleStart),
        y:Float(scaleStart),
        z:Float(scaleStart)
      )
    case .Ended:
      scaleEnd = scaleStart
      if(dbg){println("pinch ended \(scaleEnd)")}
    case .Cancelled:
      break
    case .Failed:
      break
    case .Possible:
      break
    default:
      break
    }
  }