ios - 在 TouchDown 离开控件之后、TouchUp 之前检测手指

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

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

我需要一种防错方法,以便在 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];
        }
    }
}

最佳答案

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

编辑:

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/

相关文章:

iOS-通知中心addObserver "UIMenuControllerWillHideMenu"

iphone - arrayWithContentsOfURL 的正确用法是什么?

ios - 如何从 iOS 11 beta 的 iOS 应用程序打开定位服务?

ios - UIButton 的剪辑图像

ios - 以图形方式将对象添加到核心数据

ios - 在 iOS 应用程序中添加应用程序事件会产生未声明的标识符 'AppEvent'

iphone - 核心数据内存问题

ios - 抽象实体上的核心数据关系

objective-c - 有没有办法记录 Objective-C 实例的所有属性值

iphone - scrollsToTop 不工作