1

I have a UISwipeGestureRecognizer that animates accordingly to which UIButton is highlighted at the time of the gesture, it finishes the animation when the UIButton calls TouchUpInside/TouchUpOutside. This works fine for a downward swipe gesture on my UIButton but for some reason when I try a upward swipe (on another UISwipeGestureRecognizer), it doesn't run the function. (yes it is the same UIButton) This is what I did in viewDidLoad:

    [myButton addTarget:self action:@selector(myAction) forControlEvents:(UIControlEventTouchUpInside|UIControlEventTouchUpOutside)];

For the action I just used:

    -(void) myAction{
    //Do whatever I want to do, in this case finish the animation.
    }

None of the code that I run for the swipe gesture should even effect the UIButton, so I guess I'm wondering if anyone has any idea what I could be doing wrong here?

Edit: It seems like the problem is that when I drag upwards too fast the button loses the "focus" (the highlight), therefore TouchUpInside/TouchUpOutside aren't getting called, but when I drag downwards at any speed the UIButton stays highlighted the entire time, anyway to fix this? (see GIF) Watch the highlight end before the mouse leaves the button, then when I do it again, but slower it works fine...

2 Answers2

2

From our discussion below I understand that you have declared the UIGestureRecognizers in the storyboard but also unnecessarily added them in code. Try cancelling them, see if the UIButton touches work as intended and then decide on a strategy - either create and assign them all in your storyboard or all in code.

Stavash
  • 14,244
  • 5
  • 52
  • 80
  • The `UIGestureRecognizer` shouldn't be effecting it, though I've found the "issue", when dragging upwards too fast the `UIButton` loses the "touch" (the highlight disappears) before the touch leaves the button, some how no matter how fast I touch and drag down on the `UIButton` it stays highlighted the whole time, let me upload a GIF real quick.. – Jon Sullivan May 12 '13 at 19:09
  • 1
    OK so the image helps understand the problem but I still don't get the setup - you have 2 separate swipe gesture recognizers, correct? Who are they assigned to, what's their setup and what are the relations to the view hierarchy? – Stavash May 12 '13 at 19:47
  • I have 4. Up, down, left and right. (TouchUpInside/TouchUpOutside only works when dragging down, though I haven't tested left and right yet.) & there is only one view controller and they are all assigned to it. – Jon Sullivan May 12 '13 at 21:09
  • 1
    UIGestureRecognizers can only be assigned to UIViews, so I recon you mean that they are assigned to your view controller's view? Can you add the code you're using to make this happen? – Stavash May 12 '13 at 21:43
  • All I'm doing is this: [self.view addGestureRecognizer:myRecognizer]; is that what you mean? (sorry, I'm pretty new to iOS development.) – Jon Sullivan May 13 '13 at 02:12
  • 1
    Yes, though you spoke of 4 instances. Please show your code (including the part where you create this gesture recognizers) – Stavash May 13 '13 at 06:44
  • The recognizers are already made in the storyboard, they are connected via IBOutlets and each one is added in the same way ([self.view addGestureRecognizer:myRecognizer];, four times over, one for each recognizer.) – Jon Sullivan May 13 '13 at 06:52
  • 1
    If you're doing this via storyboard, why are you connecting them in code and not directly via the interface builder? – Stavash May 13 '13 at 06:54
  • I originally was creating them in code, so when I moved them to the storyboard, I just used the same names for the IBOutlets that I used when creating them in code, but never got around to doing it in the interface builder since it was working anyway. I'm confused however, is it possible that my gesture recognizer setup is effecting the UIButtons behavior? Correct me if I'm wrong, but I'd think that even if my recognizer setup was incorrect, the UIButton should still be able to call TouchUpInside/TouchUpOutside properly, regardless of the recognizers, correct? – Jon Sullivan May 13 '13 at 06:58
  • 1
    Just to be sure - comment out the code you're using to attach the GRs and see if the button works properly – Stavash May 13 '13 at 07:02
  • Done, when I did I found out that I actually have added them in the interface builder, so the code was unnecessary anyway. So I went a step further and removed the recognizers from the storyboard and the problem seems to have "changed" without the recognizers, now it detects the TouchUpInside/TouchUpOutside at any speed but if I release my finger too far away from the UIButton nothing gets called. This is so confusing and frustrating. :/ – Jon Sullivan May 13 '13 at 07:09
  • 1
    This is good news. Now I think that if you change touchUpOutside with touchDragOutside it will work the way you intended. Also catch touchDragEnter if you want a "piano" effect. – Stavash May 13 '13 at 07:16
  • This is amazing...Same issue again, when I drag downwards a certain distance away from the button it works perfect, but no matter how far I drag upwards away from it, touchDragOutside will not get called. Thanks anyway man, I've up voted you for all the help..I'll just have to rethink my UI strategy or something :/ – Jon Sullivan May 13 '13 at 07:25
2

I've fixed it! (Finally, pretty stupid of me) The reason TouchUpInside/TouchUpOutside didn't get called was because I forgot to add

    myGesture.cancelsTouchesInView=NO;

For all my gesture recognizers, thanks anyway everyone.