我有一些UIButton
实例对其UIControlEventTouchDown
和UIControlEventTouchUpInside
事件执行不同的代码。目前我遇到了一个问题,如果用户按下,然后UIButton
在触摸之前将他们的手指拖出 's 的边界,TouchUpInside
代码不会运行,因此,下一个TouchDown
会导致崩溃。
我需要一种方法来防错,以便在 a 之后TouchDown
,TouchUpInside
如果手指在以下情况下执行代码:a)抬起按钮,b)拖离按钮,或 c)以其他方式取消。
a) 由 解决UIControlEventTouchUpInside
,并且我都尝试了UIControlEventTouchDragExit
and 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];
}
}
}