1

I'm working through the Stanford Swift Course iOS9 from 2016 and having trouble replicating a UIPinchGestureRecognizer, getting very confused with the Selector code syntax.

Inside Class FaceViewController in FaceViewConroller.Swift:

@IBOutlet weak var faceView: FaceView! {
    didSet {
        faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: #selector(changeScale(_:))))
        updateUI()
    }
}

Inside Class definition of Faceview in FaceView.Swift:

@objc
func changeScale(_ recognizer: UIPinchGestureRecognizer) {
    switch recognizer.state {
    case .changed,.ended:
        scale *= recognizer.scale
        recognizer.scale = 1.0
    default:
        break
    }
}

It builds without an error (which took a while) but when it runs the pinch gesture causes an error:

Faceit.FaceViewController changeScale:]: unrecognized selector sent to instance

Thanks.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Adam
  • 23
  • 2

1 Answers1

3

You have passed the wrong target parameter.

Since the method you want to call (changeScale) is declared in FaceView, the target should be an instance of FaceView, not FaceViewController, since FaceViewController does not have a changeScale method.

So:

faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: #selector(changeScale(_:))))
Sweeper
  • 213,210
  • 22
  • 193
  • 313