使用 UIPanGesture 时发送到实例的无法识别的选择器

Posted

技术标签:

【中文标题】使用 UIPanGesture 时发送到实例的无法识别的选择器【英文标题】:Unrecognized selector sent to instance when using UIPanGesture 【发布时间】:2015-12-23 22:32:49 【问题描述】:

您好,我正在尝试将 UIPanGestureRecognizer 添加到 UIImageView(在我的情况下,它是一个表情符号)。所有其他 UIGestureRecognizers,如长按、旋转和捏合都可以正常工作。但是,它给了我一个错误:当我添加 UIPanGestureRecognizer 时,无法识别的选择器发送到实例。我花了一天时间试图找出原因,但未能解决。请帮忙!提前致谢。

这是我将 UIGestureRecognizer 添加到贴纸的功能

  func emojiInsert(imageName: String) 

    deleteButtonHides()

    let stickerView: UIImageView = UIImageView(frame: CGRectMake(backgroundImage.frame.width/2 - 50, backgroundImage.frame.height/2 - 50, stickerSize, stickerSize))
    stickerView.image = UIImage(named: imageName)
    stickerView.userInteractionEnabled = true
    stickerView.accessibilityIdentifier = "sticker"

    let deleteStickerButton: UIImageView = UIImageView(frame: CGRectMake(stickerView.frame.width - 5 - stickerView.frame.width/3, 5, stickerView.frame.width/3, stickerView.frame.height/3))
    deleteStickerButton.image = UIImage(named: "button_back")
    deleteStickerButton.accessibilityIdentifier = "delete"
    deleteStickerButton.userInteractionEnabled = true
    deleteStickerButton.alpha = 0
    deleteStickerButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "deleteButtonTouches:"))
    stickerView.addSubview(deleteStickerButton)


    stickerView.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: "handlePinch:"))
    stickerView.addGestureRecognizer(UIRotationGestureRecognizer(target: self, action: "handleRotate:"))
    stickerView.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: "handleLongPress:"))
    stickerView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "handlePan"))

    print("emojiInsert : \(imageName)")

    backgroundImage.addSubview(stickerView)

以下是我在 view.swift 末尾添加的回调函数。我使用 touchesbegan 和 touchesMoved 来拖动表情符号,但表情符号在旋转后以奇怪的方式移动。所以现在我正在尝试使用 UIPanGesture 来拖动表情符号。

    @IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) 
    if(deleteMode) 
        return
    
    print("handlePinch \(recognizer.scale)")

    if let view = recognizer.view 
        view.transform = CGAffineTransformScale(view.transform,
            recognizer.scale, recognizer.scale)
        recognizer.scale = 1
    


@IBAction func handleRotate(recognizer : UIRotationGestureRecognizer) 
    if(deleteMode) 
        return
    
    if let view = recognizer.view 
        view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
        recognizer.rotation = 0
    



@IBAction func handlePan(recognizer:UIPanGestureRecognizer) 
    if(deleteMode) 
        return
    

    let translation = recognizer.translationInView(self.view)
    if let view = recognizer.view 
        view.center = CGPoint(x:view.center.x + translation.x,
            y:view.center.y + translation.y)
    
    recognizer.setTranslation(CGPointZero, inView: self.view)



@IBAction func handleLongPress(recognizer: UILongPressGestureRecognizer) 
    if(recognizer.state == UIGestureRecognizerState.Began) 
        if(!deleteMode) 
            print("LongPress - Delete Shows")
            for (_, stickers) in self.backgroundImage.subviews.enumerate() 
                for (_, deleteButtons) in stickers.subviews.enumerate() 
                    if let delete:UIImageView = deleteButtons as? UIImageView
                        if(delete.accessibilityIdentifier == "delete") 
                            delete.alpha = 1
                        
                    
                
            
            deleteMode = true
         else 
            deleteButtonHides()
        
    

再次,请帮助!提前致谢。

【问题讨论】:

【参考方案1】:

问题是您缺少一个冒号。在以下行中:

stickerView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "handlePan"))

handlePan 应该是 handlePan:。那是因为您的方法的 Objective-C 签名是:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer

冒号是方法名的一部分。

【讨论】:

以上是关于使用 UIPanGesture 时发送到实例的无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章

使用 NSTimer 时发送到实例的无法识别的选择器

NSInvalidArgumentException,使用 performSegueWithIdentifier 时发送到实例的无法识别的选择器

使用 UIPanGesture 将图像的位置存储到 Objc 中 UIView 中的新位置

以编程方式回调按钮时发送到实例的无法识别的选择器

在访问 PFObject 成员时解析“发送到实例的无法识别的选择器”

仅在 iPhone 上水平添加 UIPanGesture