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 に答える