Iphone 当触摸移出被触摸对象时停止NStimer

Iphone 当触摸移出被触摸对象时停止NStimer,iphone,ios,xcode,ipad,ios6,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 locatio

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

谢谢你的帮助

触摸开始
当您在屏幕上按手指时,事件将执行。如果您移动手指,则每次移动都会调用
touchesMoved
,当您抬起手指时,会调用
touchEnd

因此,基本上,如果所需的按钮被触摸到
touchMoved
方法,您应该添加测试代码

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

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