TouchesEnded和TouchesCancelled未调用

Kir*_*odd 5 uikit ios

好的,我在Xcode 4.5/iOS 6上使用选项卡式应用程序模板测试了以下内容.

  1. 创建了一个选项卡式应用程序.
  2. 创建了一个名为SampleButton的UIButton子类,并实现了以下方法:

    - (void)touchesBegan:(NSSet *)touches
               withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet *)touches
                   withEvent:(UIEvent *)event
    {
        [super touchesCancelled:touches withEvent:event];
    }
    
    - (void) touchesMoved:(NSSet *)touches
                withEvent:(UIEvent *)event
    {
        [super touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches
               withEvent:(UIEvent *)event
    {
        [super touchesEnded:touches withEvent:event];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将此SampleButton添加到第一个选项卡.

  4. 为所有触摸方法添加了断点.
  5. 在设备上运行.
  6. 测试了所有触摸方法都按预期触发.
  7. 现在触摸SampleButton,然后按第二个选项卡.

结果:视图切换到第二个选项卡,但在SampleButton中从不调用touchesCancelled和/或touchesEnded.如果在我触摸该按钮时视图发生变化,那么其中一个或那个不应该发射吗?这被证明是一个巨大的问题,因为在我的应用程序中,当按钮关闭时我正在播放声音,如果用户在按下按钮时切换标签,它将永远不会停止播放.看起来像这样在iOS3和iOS4中工作得很好.

Kir*_*odd 1

我通过设置操作目标而不是使用触摸方法来解决这个问题:

[self addTarget:self action:@selector(handleKeyPressed) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(handleKeyReleased) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(handleKeyReleased) forControlEvents:UIControlEventTouchCancel];
Run Code Online (Sandbox Code Playgroud)

即使视图交换出去,这些似乎也会正确触发。