【问题标题】:Detect a finger after TouchDown leaving the control, before TouchUp在 TouchDown 离开控件之后,在 TouchUp 之前检测手指
【发布时间】:2012-12-29 13:32:33
【问题描述】:

我有一些UIButton 实例在它们的UIControlEventTouchDownUIControlEventTouchUpInside 事件上执行不同的代码。目前我遇到了一个问题,如果用户在触摸之前将手指从UIButton 的边界中拖出,则TouchUpInside 代码不会运行,因此,下一个@ 987654326@ 导致崩溃。

我需要一种方法来防错,以便在TouchDown 之后,如果手指在以下情况下执行TouchUpInside 代码:a) 抬起按钮,b) 拖离按钮,或 c)以其他方式取消。

a) 由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];
        }
    }
}

【问题讨论】:

    标签: ios objective-c cocoa-touch touch-event uicontrolevents


    【解决方案1】:

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

    编辑:

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

    并将标志设置为 YES Before

    【讨论】:

    • 但是如果我这样做,我如何区分 Touch Down 代码和 Touch Up 代码? isTracking 只是一个布尔值。
    • 'isTracking' 每次都给 YES 除非触摸被取消或退出。所以你必须为目标方法的第一次调用设置一个标志
    • 那么,第一次调用时将其设置为 YES,那么我什么时候可以将其设置回 NO 以进行后续推送?
    • 做这样的事情 if([btn isTracking]) { if(flag) { flag=NO; //你的 TouchDown 代码 } } else { flag=YES; //你的触摸取消和退出代码}并在开始时将标志设置为YES
    • 这不起作用,在任何情况下都不会调用触摸取消代码。
    【解决方案2】:

    对于取消触摸,您可以使用 UIControlEventTouchCancel 。

    对于 b),我认为 UIControlEventTouchDragExit 应该可以工作,我已经尝试过它并且它可以正常工作,请确保您已正确连接 IBAction。

    【讨论】:

    • 我没有使用 IBActions,这一切都是以编程方式完成的,但是不,DragExit 没有按应有的方式触发。
    • 我通过 IBAction 尝试过,它可以工作,你能显示你用于添加动作/生成按钮的代码吗?
    【解决方案3】:

    回答你的观点

    b) 拖掉按钮,需要使用UIControlEventTouchDragOutside

    c) 以其他方式取消,您不需要使用UIControlEventTouchCancel

    这样:

    [newBall.button addTarget:self action:@selector(someMethod:withEvent: )
                  forControlEvents: UIControlEventTouchCancel | UIControlEventTouchDragOutside];
    

    【讨论】:

      【解决方案4】:

      为您需要的所有事件设置相同的目标。

      [btn addTarget:self action:@selector(yourMethodtoHandleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
      [btn addTarget:self action:@selector(yourMethodtoHandleTouchUpInside:) forControlEvents:UIControlEventTouchDragOutside];
      [btn addTarget:self action:@selector(yourMethodtoHandleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

      • 这就是我所做的。 DragOutside 或 Cancel 都不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2013-10-23
      相关资源
      最近更新 更多