我可以在删除发送者时调用“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")
}

}

最佳答案

问题是您没有指定选择器

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

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

希望这会有帮助

10-08 05:27