Sean Sehwan Choi
Sean Sehwan Choi

Reputation: 61

Unrecognized selector sent to instance when using UIPanGesture

Hi I am trying to add UIPanGestureRecognizer to UIImageView (in my case, it's an emoji). All other UIGestureRecognizers such as long press, rotation, and pinch work well. However, it gives me an error: unrecognized selector sent to instance when I add UIPanGestureRecognizer. I've spent a day trying to figure out the reason but failed to fix it. Please help! Thanks in advance.

This is a function where I added UIGestureRecognizer to sticker

  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)
}

Below are call back functions I added in the end of the view.swift. I used touchesbegan and touchesMoved to drag an emoji but emoji moved in weird way after rotation. So now I am trying to use UIPanGesture to drag an emoji.

    @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()
        }
    }
}

Again, please help! Thanks in advance.

Upvotes: 0

Views: 816

Answers (1)

DarkDust
DarkDust

Reputation: 92335

The problem is that you're missing a colon. In the following line:

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

The handlePan should be handlePan:. That's because the Objective-C signature for your method is:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer

The colon is part of the method name.

Upvotes: 2

Related Questions