简体   繁体   中英

Can't drag IBAction from UITapGestureRecognizer in Storyboard

I am trying to drag an IBAction from my UITapGestureRecognizer, which was placed on an UIImageView that resides in a custom tableview cell.

However, it does not allow me to drag an action from storyboard to the custom tableview cell's subclass

The cell's class is properly set to the correct file in the identity inspector. And user interaction is enabled on the imageview.

So I tried typing out the IBAction and connecting it from the file to storyboard, and it gives this error:

连接IBAction后发生错误

This is perplexing because I am able to drag IBActions from UIButtons and other view objects; just not this UITapGestureRecognizer. I have recently updated Xcode to 7.2.1

Here's my view hierarchy from the document outline:

自定义单元文档大纲

Here's the tap gesture's connections:

imageview的连接

And the imageview's connections:

在此处输入图片说明

I always added tap gesture recognizer object in code instead of working on Storyboard, now after many Xcode versions changes (currently 8.1, swift 3.0). I revisited some similar issue when trying to work with Tap Gesture Regnizer on Story board. I landed on this page even though I am working on a simple UIImageView with added Tap Gesture Recognizer object (drag and drop Tap Gesture Recognizer object into your image view). Here I listed a small step by step tutorial and hope it's useful to others who ended up on this page like me.

1) Make sure your image view has these connections in the inspector before continue

在此处输入图片说明

2) Added @IBAction response function in you swift code.

 // MARK: Actions
  @IBAction func tapGestureAction (_ sender: UITapGestureRecognizer){
    print("Tap on Flower Image View")
  }

3) On Main.storyboard, click on the Tap Gesture object either from 'Document outline' or the icons collection in your view controller scene.
在此处输入图片说明

or

在此处输入图片说明

4) Go to Connections Inspector and ctrl + click the plus sign next to 'selector' in 'Sent Actions' and drag it to view controller icon of the scene. Once release the mouse you will see the name of your function defined in step 2) show up. You can select and that should make the connection.

在此处输入图片说明

在此处输入图片说明

5) If the connection successfully made, the Connections Inspector of Tap Gesture Recognizer object should look like below

在此处输入图片说明

As beyowulf points out in the comments above, there was a change in iOS 9 that changed how UIGestureRecognizers work with repeating content.

This is unfortunate because I find it best practice to use storyboards over programmatic implementation, but if you're trying to figure this out here's the code:

In your custom cell class:

override func awakeFromNib() {
    super.awakeFromNib()

    let imageViewDoubleTap = UITapGestureRecognizer(
         target: self,
         action:#selector(ThisClass.userSwiped)
         )
    imageViewDoubleTap.numberOfTapsRequired = 2
    self.photoImageView.addGestureRecognizer(imageViewDoubleTap)
}

Then create a func with the same name you provided the action parameter and implement your code in there.

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