Learning to work with iOS, I found an example of how to perform gesture recognition

This is how it looks in the example.


enter image description here


enter image description here


And just the same, I implemented it in my implementation


import UIKit class FaceViewController: UIViewController { var expression = FacialExpression(eyes: .Open, eyeBrown: .Normal, mouth: .Smile) { didSet { updateUI() } } @IBOutlet weak var faceView: FaceView! { didSet { faceView.addGestureRecognizer(UIPinchGestureRecognizer( target: faceView, action: #selector(FaceView.changeScale(_ :)) )) updateUI() } } 

 class FaceView: UIView { @IBInspectable var scale: CGFloat = 0.9 { didSet { setNeedsDisplay() } } @IBInspectable var mouthCurvature: Double = 1.0 { didSet { setNeedsDisplay() } } @IBInspectable var eyesOpen: Bool = true { didSet { setNeedsDisplay() } } @IBInspectable var eyebrowTilt: Double = 0.5 { didSet { setNeedsDisplay() } } @IBInspectable var color: UIColor = UIColor.blue { didSet { setNeedsDisplay() } } @IBInspectable var lineWidth: CGFloat = 5.0 { didSet { setNeedsDisplay() } } func changeScale(recognizer: UIPinchGestureRecognizer) { switch recognizer.state { case .changed, .ended: scale *= recognizer.scale recognizer.scale = 1.0 default: break } } 

But I’m Type FaceView has no member changeScale error. Type FaceView has no member changeScale

and as far as I understand it is logical as I try to refer to it as a static method FaceView.changeScale , but this is the method of the object ...

But how then does this work in the example? Maybe the difference in the versions of the swift?

And then, if I make this function static, then it naturally asks for the parameter recognizer: UIPinchGestureRecognizer , but I cannot pass it because it is not initialized ...

In general, it is not clear how this works in the example and does not work for me ...

What did you do wrong?

    1 answer 1

    By the name of the transmitted parameter you should not have

     #selector(FaceView.changeScale(_:)) 

    but

     #selector(FaceView.changeScale(recognizer:)) 

    Otherwise, everything is correct. This is not a static method call.

    • yes indeed ... Thank you. I understand that this is already in Swift 3, because there is no this parameter in the example. Ok I will be more careful in the parameters - Aleksey Timoshchenko
    • @AlekseyTimoshchenko, yes, in Swift 3, small changes were made in this regard. For example, if func changeScale(recognizer: UIPinchGestureRecognizer) then so FaceView.changeScale(recognizer:) . And if func changeScale(_ recognizer: UIPinchGestureRecognizer) then so FaceView.changeScale(_:) . - VAndrJ