当触摸移出触摸的对象时停止NStimer

时间:2012-11-14 17:48:28

标签: iphone ios xcode ipad ios6

我的应用有些问题。因此,当手指移出被触摸的物体(按钮)时,我需要停止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];
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在屏幕上按手指时会执行

touchesBegan事件。如果你移动手指,每次移动都会调用touchesMoved,当你抬起手指时,会调用touchEnd

因此,基本上你应该添加用于测试的代码,如果需要按钮被touchMoved方法触及。

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