【问题标题】:Stop NStimer when touches moved out of touched object当触摸移出触摸对象时停止 NStimer
【发布时间】:2012-11-03 06:34:46
【问题描述】:

我的应用有一些问题。因此,当手指移出触摸对象(按钮)时,我需要停止 NStimer。于是就有了代码:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
    {
        CGPoint location = [touch locationInView:touch.view];

        if ([testButton.layer.presentationLayer hitTest:location]) {

            timer = 60;
            time = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(randomVoid) userInfo:nil repeats:YES];
        } else if (![testButton.layer.presentationLayer hitTest:location]){
            [time invalidate];
        }
    } 

     }

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [time invalidate];
}

感谢您的帮助!

【问题讨论】:

  • 错误或问题是什么?
  • 错误是什么?另外,添加时间 = nil; [时间失效]之后;
  • 好的,当我将手指移动到另一个对象时,计时器不会停止
  • 确保您没有多次发送计时器。
  • 时间类型是强对象吗?

标签: iphone ios xcode ipad ios6


【解决方案1】:

touchesBegan 事件在您在屏幕上按下手指时执行。如果你移动手指,每次移动都会调用touchesMoved,当你抬起手指时,会调用touchEnd

所以,基本上你应该在touchMoved 方法中添加测试所需按钮是否被触摸的代码。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![testButton.layer.presentationLayer hitTest:location]){
        [time invalidate];
        time = nil;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多