简体   繁体   English

UIButton的TouchUpInside / TouchUpOutside并非总是被调用吗?

[英]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. 我有一个UISwipeGestureRecognizer ,它可以相应地动画化手势时突出显示的UIButton ,并在UIButton调用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. 对于我的UIButton上的向下滑动手势,此方法效果很好,但是由于某些原因,当我尝试向上滑动(在另一个UISwipeGestureRecognizer )时,它无法运行该功能。 (yes it is the same UIButton ) This is what I did in viewDidLoad: (是的,它是相同的UIButton )这是我在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? 我为滑动手势运行的代码都不会影响UIButton ,因此我想知道是否有人知道我在这里可能做错了什么?

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? 编辑:似乎问题在于,当我向上拖动得太快时,按钮失去了“焦点”(突出显示),因此不会调用TouchUpInside / TouchUpOutside,但是当我以任何速度向下拖动时, UIButton突出显示整个时间,还是要解决这个问题? (see GIF) (请参见GIF) 在鼠标离开按钮之前观看突出显示的结尾,然后当我再次按下它时,但速度较慢时可以正常工作...

From our discussion below I understand that you have declared the UIGestureRecognizers in the storyboard but also unnecessarily added them in code. 从下面的讨论中,我了解到您已经在情节提要中声明了UIGestureRecognizers,但也不必要地在代码中添加了它们。 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. 尝试取消它们,查看UIButton是否按预期工作,然后确定策略-在故事板上或在代码中全部创建并分配它们。

I've fixed it! 我已经解决了! (Finally, pretty stupid of me) The reason TouchUpInside/TouchUpOutside didn't get called was because I forgot to add (最后,我非常愚蠢)未调用TouchUpInside / TouchUpOutside的原因是因为我忘记添加

    myGesture.cancelsTouchesInView=NO;

For all my gesture recognizers, thanks anyway everyone. 对于我所有的手势识别器,谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM