10

I have a UIButton connected to an IBAction in Interface Builder.

The Problem:

  • The action firing and the de-highlight of the button both take a little bit to happen.

The Facts:

  • View structure:
    • View
      • 10 Buttons (connected via UIControlEventTouchUpInside to the IBAction
      • View (Subview)
        • Gesture recognizer
        • Text Field
  • The Subview has a UITapGestureRecognizer, which delaysTouchesBegan and delaysTouchesEnded both are set to NO
  • The action is happening in the main thread.
  • Testing a simple button (with no images or title, and only a simple NSLog), the result is the same

The Question: Why are firing and the de-highlight delayed?

Natan R.
  • 5,141
  • 1
  • 31
  • 48

4 Answers4

14

In the end, I added somewhere some UIGestureRecognizer, and forgot to set delaysTouchesBegan to NO =(

Natan R.
  • 5,141
  • 1
  • 31
  • 48
  • 3
    Your answer and question helped me a lot. In my case, adding a double tap gesture on a view which contains buttons delays button reaction (os is waiting to be sure button tap won't become double tap) – The Windwaker Nov 04 '14 at 16:08
  • Thanks for this. I was able to find multiple gesture recognisers causing considerable lag button's touchupinside. – Spencer Müller Diniz Nov 28 '17 at 12:35
8

Ok I think that because of the UITapGestureRecognizer .. try to do the following :

  1. connect an IBOutlet to your button.

2.assing the UITapGestureRecognizer delegate to your ViewController.

3.Implement this gesture delegate method in yourViewController

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {
    return (! [yourButton pointInside:[touch locationInView:yourButton] withEvent:nil]);
}

This will make the tap to be recognized to your button not to the recognizer.

Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36
  • Edited again the question, @malek, check the view structure please. This method is never called when the button is pressed, only when the other subview is pressed. – Natan R. May 10 '12 at 09:42
  • Ok, this is very strange, and I have no idea why and how. **But cleaning all the build folder and the project, resolved.** – Natan R. May 10 '12 at 15:44
  • Thanks! This may not have helped @NatanR., but it was exactly what I needed. – Dave Batton Nov 09 '12 at 23:24
  • @Malek_Jundi, just note that it's not **UITapGestureRecognizer** but **UIGestureRecognizerDelegate** instead (point 2.)... – Despotovic Dec 19 '13 at 11:31
0

Make sure your touch event is set the first contact of the button which would be the touch down event otherwise there will be a delay in the action until whichever other event you chose gets completed (i.e. touch up inside, touch up outside, etc).

Nathan C.
  • 319
  • 3
  • 8
  • If that doesn't do it then I'd have to know more specifics of the code to give you a winning answer. – Nathan C. May 10 '12 at 09:22
  • Ok, this is very strange, and I have no idea why and how. **But cleaning all the build folder and the project, resolved.** – Natan R. May 10 '12 at 15:44
  • Ah, glad you figured it out. I'm so used to doing that when a project doesn't work as it should that I forgot to suggest it. Basically your simulator was running on cached code. – Nathan C. May 10 '12 at 16:19
0

In my case, there was a delay on IBAction for a button that was in a custom CalloutView of an MKAnnotationView.

In the same way there is a ~0.5sec delay between pressing the MKAnnotationView and the MKAnnotationView actually being selected, there is also a delay between any other user interactions you might add as a subview of the MKAnnotationView.

The solution is to disable the native UIGestureRecognizer within MapView that is causing the delay of any MKAnnotation view selections.

This can be done with the solution on this post:

Set isZoomEnabled = false within a gesture recognizer attached to the mapview on any tap, then set isZoomEnabled = false within a 0.5sec async dispatch.

Plato
  • 111
  • 2
  • 7