Search code examples
swiftfunctionmethodsparametersgesture

Swift UIGestureRecognizer Notation


I'm trying to learn Swift, and this has me really confused:

Say you're adding a gesture:

        faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:")) 

And this is the scale function:

func scale(gesture: UIPinchGestureRecognizer) {

    if gesture.state == .Changed {
        scale *= gesture.scale
        gesture.scale = 1
    }

}

Why is there a colon at the end of scale (e.g. action:"scale:")? Is it to reference the fact that the scale function takes in a parameter of type UIPinchGestureRecognizer?

If it is, then how does the colon represent that parameter?


Solution

  • The gesture recognizer is the parameter. By specifying a colon you say you want to have the type (the recognizer) sent as a parameter, just as you thought. If you omit said colon, you would need to have a function without the recognizer as a parameter.