【发布时间】:2012-12-29 13:32:33
【问题描述】:
我有一些UIButton 实例在它们的UIControlEventTouchDown 和UIControlEventTouchUpInside 事件上执行不同的代码。目前我遇到了一个问题,如果用户在触摸之前将手指从UIButton 的边界中拖出,则TouchUpInside 代码不会运行,因此,下一个@ 987654326@ 导致崩溃。
我需要一种方法来防错,以便在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];
}
}
}
【问题讨论】:
标签: ios objective-c cocoa-touch touch-event uicontrolevents