14

I guys,

today I've updated my iPhone to iOS 9 and now have problems with a gesture recognizer. This is the error:

WARNING: A Gesture recognizer (; target= <(action=onVideoTap:, target=)>>) was setup in a storyboard/xib to be added to more than one view (->; layer = >) at a time, this was never allowed, and is now enforced. Beginning with iOS 9.0 it will be put in the first view it is loaded into.

I didn't have this problem with iOS8. The view contains a UIImageView and a TextView. The recognizer was dropped into the ImageView and has only referncing outlets to this view.

I don't really understand this issue. Can somebody help me? Thank you :)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2529173
  • 1,884
  • 6
  • 30
  • 43

3 Answers3

13

This was happening with me because I wanted to use a Tap Gesture Recognize with an image in a TableViewCell contained in a TableView.

The problem is that:

I add one Tap Gesture Recognizer but I have more than one TableViewCell (more than one image) in my table.

iOS will assign the UITapGestureRecognizer to the first image in the first cell, and other cells will be without gestures (the gesture already set to the first image only).

To Solve this problem follow this:

  1. Make sure you checked User Interaction Enabled for the UIView you want to assign with a TapRecognizerGesture.
  2. in the sub-view TableViewCell in my case add a new UITapGestureRecognizer. The code:

    internal let tapRecognizer1: UITapGestureRecognizer = UITapGestureRecognizer()`
    
  3. In your main view TableView in my case and for every cell assign the UITapGestureRecognizer you made with each cell to a main function in the main view:

    cell.tapRecognizer1.addTarget(self, action: "img_Click:")
    cell.img.gestureRecognizers = []
    cell.img.gestureRecognizers!.append(cell.tapRecognizer1)
    
  4. Add the function you want UITapGestureRecognizer to fire when clicked:

    func img_Click(sender: UITapGestureRecognizer) {
        print("ok")
    }
    

Notes:

  • You can use simple way if you do not want the UITapGestureRecognizer action in the main view by assigning it in its sub-view directly.
  • in step 4 function name must be the same as in addTarget line.
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
Wajih
  • 4,227
  • 2
  • 25
  • 40
  • I had to use the following code when adding the target to the recognizer: `cell.tapRecognizer.addTarget(self, action: #selector(FeedController.imageTap(_:)))` – Gustavo Straube Jun 14 '16 at 15:47
  • 1
    @GustavoStraube Yes you are right, This upgrade had been released in the new `swift`, But the older one has the same as I answer. Thanks. – Wajih Jun 15 '16 at 05:11
12

I think that this problem happen when you use storyboard added a Tap Gesture Recognizer. Because some reasons you added more than one views.(see the picture).

enter image description here

So, delete other wrong views,leave the right one view.

enter image description here

saneryee
  • 3,239
  • 31
  • 22
4

Already fixed it. The storyboard is localized and in one language I assigned the the recognizer twice to the picture view. Somehow this seemed to cause troubles on the other storyboards too.

user2529173
  • 1,884
  • 6
  • 30
  • 43
  • im facing the same issue..what do you mean by you assigned it twice to the picture view..can you please explain..will be really helpful – Anuj Arora Oct 11 '15 at 09:55
  • One recognizer can anly be assigned to one other component. For me, I assigned the same recognizer to the picture view twice. Just have a look at the recognizer and see to which other components it's reffering – user2529173 Oct 11 '15 at 12:50
  • 3
    This is unfortunate - I've encountered the same problem because I've purposely been using the same gesture recognizer on multiple components, which seems like a reasonable thing to do. I guess this means if I want to attach the same behavior to multiple objects then instead of using a single gesture recognizer I need to create multiple gesture recognizers. Feels inefficient to me. – Derek Lee Oct 30 '15 at 08:42