Search code examples
iosobjective-ctouch

Count the seconds for long holding touch


I'm trying to count the seconds for holding a tap on the screen or a specific view object.

There are some touch events like touchDownInside, touchUpInside, touchesBegan, and many more, but what method can perform holding touch?


Solution

  • Create the following two methods and add the property. Comment the button "Touch Down" event to the touchDown: method and the "Touch Up Inside" event to the touchUp: method.

    @property (strong, nonatomic) NSDate *touchDownTime;
    
    - (IBAction)touchDown:(id)sender {
        self.touchDownTime = [NSDate date];
    }
    
    - (IBAction)touchUp:(id)sender {
        NSTimeInterval timeDown = [self.touchDownTime timeIntervalSinceNow];
        NSLog(@"'timeDown: %0.1f sec", timeDown);
    }
    

    If you need to do something every second (or some interval) start a timer in touchDown and invalidate in touchUp.