我有一些 UIButton
实例,它们在它们的 UIControlEventTouchDown
和 UIControlEventTouchUpInside
事件上执行不同的代码。目前我遇到了一个问题,如果用户按下,然后在触摸之前将他们的手指拖出 UIButton
的边界,TouchUpInside
代码是' t 运行,因此,下一个 TouchDown
会导致崩溃。
我需要一种防错方法,以便在 TouchDown
之后,如果手指处于以下状态,则执行 TouchUpInside
代码:a) 抬起按钮, b) 拖离按钮,或 c) 以其他方式取消。
a) 由 UIControlEventTouchUpInside
解决,我已经尝试了 UIControlEventTouchDragExit
和 UIControlEventTouchUpOutside
,但我无法得到情况 b)或 c) 已解决。
知道我该如何处理吗?这是我的代码:
[newBall.button addTarget: self action: @selector(buttonDown:) forControlEvents: UIControlEventTouchDown];
[newBall.button addTarget: self action: @selector(buttonUp:) forControlEvents: UIControlEventTouchUpInside];
- (void) buttonDown: (id) sender
{
NSLog(@"Finger down on button %d!", [sender tag]);
int senderTag = [sender tag];
for (CBBall *i in balls) {
int currentTag = [i.button tag];
if (currentTag == senderTag) {
i.body -> f = cpvzero;
[i replaceDynamicBall: i withStaticOneAtLocation: cpBodyGetPos(i.body)];
[i setIsBeingTouched: YES];
}
}
}
- (void) buttonUp: (id) sender
{
NSLog(@"Finger up on button %d!", [sender tag]);
int senderTag = [sender tag];
for (CBBall *i in balls) {
int currentTag = [i.button tag];
if (currentTag == senderTag) {
[i replaceStaticBall: i withDynamicOneAtLocation: cpBodyGetPos(i.body)];
[i setIsBeingTouched: NO];
}
}
}
最佳答案
为 UIControlEventAllTouchEvents
设置目标,然后在目标方法中检查 UIbutton
的 isTracking
属性的值
这将解决您的问题。
编辑:
if([btn isTracking])
{
if(flag)
{
flag=NO;
//Your TouchDown Code
}
}
else
{
flag=YES;
//your Touch Cancel and Exit code
}
并在之前将标志设置为 YES
关于ios - 在 TouchDown 离开控件之后、TouchUp 之前检测手指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14337950/