0

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

谢谢你的帮助!

4

1 回答 1

0

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

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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![testButton.layer.presentationLayer hitTest:location]){
        [time invalidate];
        time = nil;
    }
}
于 2012-11-14T20:59:45.840 回答