简体   繁体   中英

UIButton TouchUpInside/TouchUpOutside not always called?

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) 在鼠标离开按钮之前观看突出显示的结尾,然后当我再次按下它时,但速度较慢时可以正常工作...

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.

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.

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