简体   繁体   中英

How do I call GestureRecognizer method programatically on Swift 4.0?

I'm doing those Stanford course, and on that part about gestures I couldn't replicated the pinchGesture, it simply doesn't works and even don't call the breakpoint inside the method that are supposed to be called. I'm copying my code below

inside FaceViewController

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

inside FaceView

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

I'm on Xcode 9 and swift is version 4.

Many thanks.

I did not see at the beginning that the function you are calling was living inside the FaceView class.

Your code works for me. Are you sure your faceView is connected to your @IBOulet ??

Should look like this:

在此处输入图片说明

To connect it you need to right-click on your view and drag it to your IBOutlet definition

设置,faceView.isUserInteractionEnabled = true,它将起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM