在TouchUp离开控件之后,在TouchUp之前检测手指

Luk*_*uke 2 cocoa-touch objective-c touch-event ios uicontrolevents

我有一些UIButton实例在他们UIControlEventTouchDownUIControlEventTouchUpInside事件上执行不同的代码.目前我正在遇到一个问题,如果用户降落,然后UIButton在触摸之前将他们的手指拖出界限,TouchUpInside代码就不会运行,因此,下一个TouchDown会导致崩溃.

我需要一种防错的方法,以便在a之后TouchDown,TouchUpInside如果手指是:a)执行代码,执行代码:a)按下按钮,b)拖出按钮,或c)以其他方式取消.

一)由解决UIControlEventTouchUpInside,我都试过UIControlEventTouchDragExitUIControlEventTouchUpOutside,但我不能让情况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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sar*_*ngh 5

设置目标UIControlEventAllTouchEvents然后在目标方法中检查isTracking属性的值UIbutton 这将解决您的问题.

编辑:

if([btn isTracking])
{
    if(flag)
    {
       flag=NO;
       //Your TouchDown Code
    }
}
else
{
   flag=YES;
   //your Touch Cancel and Exit code
}
Run Code Online (Sandbox Code Playgroud)

并将标志设置为YES之前