以编程方式添加视图和手势 - “无法识别的选择器发送到实例”

时间:2016-09-24 21:52:49

标签: swift swift3 uipangesturerecognizer unrecognized-selector

删除发件人后,我可以调用“handleTap”函数,但只要我重新添加sender: UIPanGestureRecognizer,就会收到此错误:unrecognized selector sent to instance。我无法理解!

class DrawingViewController: UIViewController, UIGestureRecognizerDelegate {

var newlyAddedSticker = UIImage() {
    didSet {
        makeNewSticker()
    }
}

func makeNewSticker() {
    let newSticker = UIImageView(image: newlyAddedSticker)
    view.addSubview(newSticker)

    let myPanGesture = UIPanGestureRecognizer(target: self, action: "handleTap:")
    myPanGesture.delegate = self
    newSticker.isUserInteractionEnabled = true
    newSticker.addGestureRecognizer(myPanGesture)
}

func handleTap(sender: UIPanGestureRecognizer) {
    print("hi")
}

}

1 个答案:

答案 0 :(得分:1)

问题是你没有指定选择器

 let myPanGesture = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.handleTap))

func handleTap() {
    print("hi")
}

希望这会有所帮助